![](/img/trans.png)
[英]How can I recreate this plot of a pandas DataFrame, line and bar
[英]How can I plot a bar with totals without the header in Pandas dataframe?
我想要 plot 一個帶有基於我的 df 的總標簽的欄。 但是,總數出現了一行。 plot 包括 header 行,因此總數放錯了地方。 這是 header 以及我的 df 和我的代碼的前 3 行:
Ethnicity Bachelor Master Doctorate
1 Chinese 278270 101555 21015
2 Black 82855 38475 6350
3 Filipino 182845 8845 885
ax = df.plot(kind="bar", stacked=True, figsize=(15,6), width=.5)
totals = df.sum(axis=1)
y_offset = 4
for i, total in enumerate(totals):
ax.text(totals.index[i], total + y_offset, round(total), ha="center")
這是因為您的數據框的索引從 1 開始。因此,您需要將文本的 x 坐標偏移 1:
ax.text(totals.index[i]-1, total + y_offset, round(total), ha="center")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.