用 Matplotlib 作图,需要纵轴显示为百分比,网上查到的方法都比较麻烦,这里给一种简单的方法。
先引入包:
from matplotlib import pyplot as plt
from matplotlib import ticker
然后参照如下代码,data
存储的是数据,取值在 0 到 1 之间,导入过程略:
fig, ax = plt.subplots()
ax.plot(data)
ax.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
plt.show()
得到的结果如下:
这里出现的两个参数:
xmax
:指定 100% 对应原始数据的值,默认值是 100,由于我们的数据是 0~1 之间的小数,所以这里要设置为 1,即 data 中的 1 表示 100%;decimals
:指定显示小数点后多少位,默认是由函数自动确定,这里我们设置成 1,使之仅显示小数点后 1 位。