This is a few months late, but I have created PR#6251 with matplotlib to add a new PercentFormatter
class. With this class you just need one line to reformat your axis (two if you count the import of matplotlib.ticker
):
import ...
import matplotlib.ticker as mtick
ax = df['myvar'].plot(kind='bar')
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
PercentFormatter()
accepts three arguments, xmax
, decimals
, symbol
. xmax
allows you to set the value that corresponds to 100% on the axis. This is nice if you have data from 0.0 to 1.0 and you want to display it from 0% to 100%. Just do PercentFormatter(1.0)
.
The other two parameters allow you to set the number of digits after the decimal point and the symbol. They default to None
and '%'
, respectively. decimals=None
will automatically set the number of decimal points based on how much of the axes you are showing.
Update
PercentFormatter
was introduced into Matplotlib proper in version 2.1.0.