我有一个包含多个列和multiIndex的Pandas DataFrame。我想将两列(“总计”和“已售出”)中的数据绘制为不同的折线图,并使用第三列“百分比”中的值作为“已售出”图表上各点的注释文本。 最好的方法是什么?任何建议和建议将不胜感激。
#data is a dict
data = { 'Department': ['Furniture','Furniture','Furniture',
'Gifts','Gifts','Gifts'],
'Month':['May','June','July','May','June','July'],
'Total':[2086,1740,1900,984,662,574],
'Sold':[201,225,307,126,143,72],
'Percentage':[10, 13, 16, 13, 22, 13]
}
# DataFrame() turns the dict into a DataFrame
# Set up MultiIndex
df=pd.DataFrame(data)
df.set_index(['Department', 'Month'], inplace=True)
df
数据帧
# Plot departments
departments=df.index.get_level_values(0).unique()
for department in departments:
ax=df.ix[department].plot(title=department,y=['Total','Sold'],
xlim=(-1.0, 3.0))
来自DataFrame的绘图
答案 0 :(得分:1)
You could achieve this in different ways.
I will just mention a couple, the most straightforward ones without the goal of being complete and I am sure there are many easier ways to do that.
One way involves the use of the method text.
In your case would be
ii = [0, 1, 2] # the locations of the month labels, according to your plotting... I leave it to you to automatize or find a way to retrieve those
for department in departments:
ax=df.ix[department].plot(title=department,y=['Total','Sold'], xlim=(-1.0, 3.0))
for c, months in enumerate(unique_list_of_months): # in your case would be ['May', 'June', 'July']
ax.text(ii[c], df.ix[department]['Sold'][c], str(df.ix[department]['Percentage'][c]) + '%')
The other method involves the use of annotate. Leaving out some for loops as above, you would replace the call to ax.text
with something like
ax.annotate(str(df.ix[department]['Percentage'][months]) + '%',
(ii[c], df.ix[department]['Sold'][months]),
xytext=(0, 0),
textcoords='offset points')
Of course you can tweak positions, font size, etc.
For an intro to annotations, please consult the official webpage:
Matplotlib annotations