Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reconstruction figure legend in pandas

Tags:

python

pandas

after plotting a figure I get a figure legend as 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 delete the 'Temp' in (Temp,2005) ,let become just 2005?

the DataFrame1 has three keys:Month,Year,Temp.

like image 342
wuwucat Avatar asked Dec 14 '12 20:12

wuwucat


People also ask

How do I label axis in pandas?

You can set the labels on that object. Or, more succinctly: ax. set(xlabel="x label", ylabel="y label") . Alternatively, the index x-axis label is automatically set to the Index name, if it has one.

Does pandas use Matplotlib?

Pandas uses the plot() method to create diagrams. We can use Pyplot, a submodule of the Matplotlib library to visualize the diagram on the screen.


1 Answers

You were very close, you just need to update your legend with the 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()
like image 194
Andy Hayden Avatar answered Oct 05 '22 12:10

Andy Hayden