0
我正在绘制一个带有Pandas
的条形图,但我想绘制条形本身上每个条的值。我该怎么做?如何在大熊猫的条形图中绘制条形图上的值?
这是我想要达到的一个例子,但是我当然希望数字在横条上不是水平的,因为我在这里。
我正在绘制一个带有Pandas
的条形图,但我想绘制条形本身上每个条的值。我该怎么做?如何在大熊猫的条形图中绘制条形图上的值?
这是我想要达到的一个例子,但是我当然希望数字在横条上不是水平的,因为我在这里。
下面是一个例子:
import pandas as pd
import matplotlib.pyplot as plt
ix3 = pd.MultiIndex.from_arrays([['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b'], ['foo', 'foo', 'bar', 'bar', 'foo', 'foo', 'bar', 'bar']], names=['letter', 'word'])
df3 = pd.DataFrame({'data1': [3, 2, 4, 3, 2, 4, 3, 2], 'data2': [6, 5, 7, 5, 4, 5, 6, 5]}, index=ix3)
gp3 = df3.groupby(level=('letter', 'word'))
means = gp3.mean()
errors = gp3.std()
fig, ax = plt.subplots()
means.plot(yerr=errors, ax=ax, kind='bar')
for rect in ax.patches:
bbox = rect.get_bbox()
x = 0.5 * (bbox.x0 + bbox.x1)
y = 0.5 * (bbox.y0 + bbox.y1)
text = "{:g}".format(bbox.y1)
ax.text(x, y, text,
va="center", ha="center",
rotation=90, fontsize="x-large", color="w")
的outtput: