[PYTHON] Show axes as percentages in Matplotlib

It's not a big deal, but it's easy to forget.

Sample data

import numpy as np

arr = np.random.randn(10)
print(arr)
# => array([ 0.24671671, -0.8013258 , -0.29147271, -0.10755521, -1.39065478,
#           -1.03983494, -0.75304377,  0.62645801,  0.76417769, -0.31104797])

When plotted normally

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.bar(x=range(len(arr)), height=arr)
ax.set_title('Some percentage')

image.png

It is difficult to understand that this is a ratio.

When displaying as a percentage

import matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.bar(x=range(len(arr)), height=arr)
ax.yaxis.set_major_formatter(matplotlib.ticker.PercentFormatter(1.0))
ax.set_title('Some percentage')

image.png

matplotlib.ticker — Matplotlib documentation

Recommended Posts

Show axes as percentages in Matplotlib
Show dividing lines in matplotlib histogram
Put matplotlib in Centos7.
Show pyramids in Python
View images in Matplotlib
2D plot in matplotlib
Adjust axes with matplotlib
Show decimal point in Python
Embed matplotlib graphs in Tkinter
Show Django ManyToManyField in Template