Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add text to pandas dataframe plot

I am plotting a dataFrame and I want to add information about the information (mean and std of the data) I am plotting the data this way:

df = pd.DataFrame({'type': lifeExpExcel['Country'], 'Infant mort. rate':
lifeExpExcel['Infant mort. rate']})
ax = df.plot(kind='bar',x= lifeExpExcel['Country'])
ax.set_ylabel('Infant mort. rate')
ax.set_xlabel('Country')
plt.show()

I want to add two string (val + name) to the plot How can I do this?

btw if there's a better way to do the plot i'll like to know that

like image 723
Shai Zarzewski Avatar asked Mar 09 '14 20:03

Shai Zarzewski


People also ask

How do you annotate in pandas plot?

Create df using DataFrame with x, y and index keys. Create a figure and a set of subplots using subplots() method. Plot a series of data frame using plot() method, kind='scatter', ax=ax, c='red' and marker='x'.

How do I create a label 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.

What is Panda rot?

rot : This keyword argument allows to rotate the markings on the x axis for a horizontal plotting and y axis for a vertical plotting. fontsize : Allows to set the font size for the labels and axis points. colormap : This keyword argument allows to choose different colour sets for the plots.


1 Answers

You can use ax.text() or ax.annotate()

ax.text accepts the the x-coord and y-coord (in data coordinates) and then the string as arguments. ax.annotate is a little more complicated: http://matplotlib.org/1.3.1/users/annotations_intro.html

like image 79
Paul H Avatar answered Oct 05 '22 04:10

Paul H