【发布时间】:2017-12-14 04:09:33
【问题描述】:
我正在从 DataFrame 创建一个简单的条形图。 (Series 和 DataFrame 上的 plot 方法只是 pyplot.plot 的简单包装)
import pandas as pd
import matplotlib as mpl
df = pd.DataFrame({'City': ['Berlin', 'Munich', 'Hamburg'],
'Population': [3426354, 1260391, 1739117]})
df = df.set_index('City')
ax = df.plot(kind='bar')
现在我想访问各个栏。我注意到的是,还有一个额外的条(矩形),宽度=1,高度=1
rects = [rect for rect in ax.get_children() if isinstance(rect, mpl.patches.Rectangle)]
for r in rects:
print(r)
输出:
Rectangle(xy=(-0.25, 0), width=0.5, height=3.42635e+06, angle=0)
Rectangle(xy=(0.75, 0), width=0.5, height=1.26039e+06, angle=0)
Rectangle(xy=(1.75, 0), width=0.5, height=1.73912e+06, angle=0)
Rectangle(xy=(0, 0), width=1, height=1, angle=0)
我希望这里只有三个矩形。第四个的目的是什么?
【问题讨论】:
标签: python pandas matplotlib plot