Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Percentage sign in matplotlib on y-axis

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()
like image 226
Slartibartfast Avatar asked Sep 20 '25 08:09

Slartibartfast


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

like image 166
Ann Zen Avatar answered Sep 22 '25 21:09

Ann Zen


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)
like image 44
r-beginners Avatar answered Sep 22 '25 21:09

r-beginners



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!