Skip to content Skip to sidebar Skip to footer

Plotting Values From Two Datasets For Comparison

I would like to plot two dataframes in order to compare the results. My first choice would be to plot line charts based only on one column from the two dataframes. df Name S

Solution 1:

IIUC,

fig, ax = plt.subplots()
ax2 = ax.twinx()

df.plot(x="Name", y=["F"], ax=ax)
df1.plot(x="Name", y=["F"], ax=ax2, ls="--")
fig.legend(loc="upper right", bbox_to_anchor=(1,1), bbox_transform=ax.transAxes)

Output:

enter image description here

Solution 2:

The simplest way to add information about other parameters to a graph is to use functions like ax.text or ax.annotate over a loop. The code should look like this:

fig, ax = plt.subplots()
data1 = ax.bar(20*index, df["F"], bar_width)
data2 = ax.bar(20*index+bar_width, df1["F"],bar_width)

for i in index:
    ax.text(i*20-5,0,df['Surname'][i],)
    ax.text(i*20-5,0.05,df['Name'][i])
    ax.text(i*20+bar_width-5,0,df1['Surname'][i])
    ax.text(i*20+bar_width-5,0.05,df1['Name'][i])
plt.show()

Image generated by the code

Useful link: Official Documentation for Text in Matplotlib Plots

Edit: Probably similar problem: Different text at each point

Edit 2: Code without index:

fig, ax = plt.subplots()
data1 = ax.plot(df["F"])
data2 = ax.plot(df1["F"])

for i in range(1,10):
    ax.text(i,df["F"][i],df['Name'][i]+" "+df['Surname'][i],)
    ax.text(i,df["F"][i],df['Name'][i]+" "+df['Surname'][i],)
plt.show()

Post a Comment for "Plotting Values From Two Datasets For Comparison"