Reputation: 4554
I have plotted a bar plot. I want the yaxis display values in percentage.
My code:
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()
Present output:
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
AttributeError: 'BarContainer' object has no attribute 'yaxis'
Upvotes: 1
Views: 4474
Reputation: 150725
plt.bar
does not return the axis instance. I think you mean:
ax = df.plot.bar(x='name',y='qty')
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
Or
fig, ax = plt.subplots()
ax.bar(df['name'],df['qty'])
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
Output:
However, guessing from you trying to plot percentage, I think you want:
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:
Upvotes: 1