Python Matplotlib:在堆叠条形图中显示列名称
给定一个数据帧:Python Matplotlib:在堆叠条形图中显示列名称,python,pandas,matplotlib,Python,Pandas,Matplotlib,给定一个数据帧: response_min \ device_mac 0dd8d 1f3cc 61ff6 623ce datetime 2016-05-25 08:00:00
response_min \
device_mac 0dd8d 1f3cc 61ff6 623ce
datetime
2016-05-25 08:00:00 0.000000 4.250000 0.250000 0.000000
2016-05-25 12:00:00 0.000000 0.000000 0.000000 0.000000
2016-05-25 16:00:00 0.000000 0.000000 0.000000 0.000000
2016-05-26 08:00:00 12.133333 119.666667 0.250000 0.000000
通过这样做:
ax = df.plot(kind='bar',stacked=True,logy=True)
for p in ax.patches:
ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005))
我得到:
如何继续显示列名称而不是条内的值
非常感谢
(剧透警报)根据Stanley的建议和调试器的帮助,我成功地做到了这一点(注意:对数刻度已关闭):
类似于这里:
import pandas as pd
import matplotlib.pyplot as plt
df=pd.DataFrame({'Devices': [ 'dev1', 'dev2', 'dev3', 'dev4'],
'Response': [4.25,0.,119.,12.1],})
df = df.set_index('Devices')
df.plot(kind='bar', title='Response')
ax = df.plot(kind='bar', title='Response')
ax.set_ylim(0, 130)
for i, label in enumerate(list(df.index)):
height = df.ix[label]['Response']
ax.annotate(str(label), (i, height + 0.3))
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
df=pd.DataFrame({'Devices': [ 'dev1', 'dev2', 'dev3', 'dev4'],
'Response': [4.25,0.,119.,12.1],})
df = df.set_index('Devices')
df.plot(kind='bar', title='Response')
ax = df.plot(kind='bar', title='Response')
ax.set_ylim(0, 130)
for i, label in enumerate(list(df.index)):
height = df.ix[label]['Response']
ax.annotate(str(label), (i, height + 0.3))
plt.show()