Reconstruction of the legend of the figure in pandas

after drawing, I get the legend of the figure, as shown below: enter image description here

DataFrame1.plot(legend=False) patch,labels=ax.get_legend_handels_labels() DateFrame1.legend(loc='best') plt.show() 

How can I remove "Temp" in (Temp, 2005) to become only 2005?

DataFrame1 has three keys: month, year, pace.

+6
source share
2 answers

You were very close, you just need to update your legend for years:

 ax = df.plot() years = [2005, 2007, 2008, 2009, 2011, 2012] # you can get years from you dataframe (but without seeing the dataframe I can't say exactly how) # legend also accepts a Series or numpy array ax.legend(years, loc='best') plt.show() 
+5
source

A unique method for each year:

 DataFrame1.plot(legend=False) patch,labels=ax.get_legend_handels_labels() DateFrame1.legend(str(unique(DataFrame1['Year'].value)),loc='best') plt.show() 

so that it works correctly.

0
source

All Articles