【Python】matplotlib直方图纵轴显示百分比


其实很简单,就是算了一下百分比权重,乘以了一个权重值

import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
from numpy.random import randn
from matplotlib.font_manager import FontProperties
def to_percent(y,position):
    return str(100*y)+"%"#这里可以用round()函数设置取几位小数

font=FontProperties(fname='/Library/Fonts/Songti.ttc')#这里设置字体,可以显示中文
x=randn(1000)
plt.hist(x,bins=30,weights=[1./len(x)]*len(x))#这里weights是每一个数据的权重,这里设置是1,weights是和x等维的列表或者series
fomatter=FuncFormatter(to_percent)
plt.gca().yaxis.set_major_formatter(fomatter)

plt.title("频率分布直方图")
plt.show()


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM