Home > Software design >  Simple label in Matplotlib
Simple label in Matplotlib

Time:07-05

I am trying to label different data in a figure and use the standard definition label='test'!

ax = df.plot(style=['tab:blue'],linewidth=1.0, label ='test')
dfh.plot(ax=ax, style=['tab:orange'], linewidth=1.0, label="line1")
leg =plt.legend()
plt.show()

But as you can see this does not work!

enter image description here

what am I doing wrong?

CodePudding user response:

fig, axes = plt.subplots(1, 1)
axes.plot(df, style=['tab:blue'], linewidth=1.0, label='test')
axes.plot(dfh, style=['tab:orange'], linewidth=1.0, label='line1')
axes.legend()

CodePudding user response:

As per the matplotlib.pyplot.legend docs (https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html)

ax.plot([1, 2, 3], label='Inline label')
ax.legend()

Your code should be:

ax.plot(df, style=['tab:blue'],linewidth=1.0, label ='test')
ax.plot(dfh, style=['tab:orange'], linewidth=1.0, label="line1")
ax.legend()
plt.show()
  • Related