Percentage sign in matplotlib on y-axis

Viewed 7760

I have the following pandas plot:

enter image description here

Is it possible to add '%' sign on the y axis not as a label but on the number.
Such as it would show instead of 0.0 it would be 0.0% and so on for all the numbers?

Code:

import pandas as pd
from pandas import datetime
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime 
end = datetime.date.today()
start = datetime.date(2020,1,1)

data = web.DataReader('fb', 'yahoo', start, end)
data['percent'] = data['Close'].pct_change()
data['percent'].plot()
2 Answers

Here is how you can use matplotlib.ticker:

import matplotlib.pyplot as plt
import matplotlib.ticker as mtick

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.show()

Output:

enter image description here

You can now control the display format of the y-axis. I think it will be 0.0%.

yvals = ax.get_yticks()
ax.set_yticklabels(["{:,.1%}".format(y) for y in yvals], fontsize=12)
Related