• 【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()
    
  • 相关阅读:
    Alpha冲刺总结
    测试随笔
    项目Alpha冲刺Day11
    项目Alpha冲刺Day12
    项目Alpha冲刺Day10
    项目Alpha冲刺Day9
    项目Alpha冲刺Day6
    beta冲刺1
    Beta准备博客
    基于微信小程序的实验室管理的postmortem
  • 原文地址:https://www.cnblogs.com/kinologic/p/14860594.html
Copyright © 2020-2023  润新知