[英]Python Bar plot y axis display percentage
我已经绘制了一个条形 plot。 我想要 yaxis 以百分比显示值。
我的代码:
import matplotlib.ticker as mtick
df =
name qty
0 Aple 200
1 Bana 67
2 Oran 10
3 Mang 8
ax=plt.bar(df['name'],df['qty'])
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.show()
目前output:
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
AttributeError: 'BarContainer' object has no attribute 'yaxis'
plt.bar
不返回轴实例。 我想你的意思是:
ax = df.plot.bar(x='name',y='qty')
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
或者
fig, ax = plt.subplots()
ax.bar(df['name'],df['qty'])
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
Output:
但是,从您尝试 plot百分比猜测,我认为您想要:
fig, ax = plt.subplots()
# note the division here
ax.bar(df['name'],df['qty']/df['qty'].sum())
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
Output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.