How to change numerical values to be formatted as percent on y-axis of python bar chart? Visualizing Earnings Based On Majors - guided project

# pandas.DataFrame.plot.bar
recent_grads[:10].sort_values("ShareWomen", ascending=False).plot.bar(x='Major', y='ShareWomen',
                              title='Share of Women in Top 10 Majors by Median Salary',
                              color=[(0.1, 0.5, 0.6), (0.1, 0.2, 0.7), (0.4, 0.7, 0.1)]
                              , legend=False)

plt.ylabel('Share of Women')
plt.xlabel('')

I wonder how to change these numbers on the y-axis into percentages?

0.1, 0.2, 0.3…-------> have it as 10,20,30…50 ?----plt.ylabel(‘Share of Women %’)

@jamesberentsen

Hey, this should work. Please modify your code as per below.

ax =  recent_grads[:10].sort_values("ShareWomen", ascending=False).plot.bar(x='Major', y='ShareWomen',
                                  title='Share of Women in Top 10 Majors by Median Salary',
                                  color=[(0.1, 0.5, 0.6), (0.1, 0.2, 0.7), (0.4, 0.7, 0.1)]
                                  , legend=False)

vals = ax.get_yticks()
ax.set_yticklabels([int((x*100)) for x in vals]) 

We are just multiplying y-ticks values with 100.

2 Likes

Thank Jenil,

I found another solution on stackoverflow too.

https://stackoverflow.com/questions/53577630/how-to-make-pareto-chart-in-python

import matplotlib.ticker as mtick



ax =  recent_grads[:10].sort_values("ShareWomen", ascending=False).plot.bar(x='Major', y='ShareWomen',
                                  title='Share of Women in Highest 10 Median Salary Majors',
                                   color=[(0.1, 0.3, 0.7), (0.1, 0.4, 0.7), (0.4, 0.5, 0.8)
                                    ,(0.1, 0.5, 0.9), (0.2, 0.5, 0.9), (0.3, 0.5, 0.9)
                                    ,(0.2, 0.8, 0.9), (0.3, 0.8, 0.9), (0.4, 0.8, 0.9),
                                     (0.4, 0.9, 0.9)]
                                  , legend=False)

#vals = ax.get_yticks()
#ax.set_yticklabels([int((x*100)) for x in vals]) 

ax.yaxis.set_major_formatter(mtick.PercentFormatter(1.0))
1 Like