Setting a relative frequency in a matplotlib histogram

后端 未结 2 499
攒了一身酷
攒了一身酷 2020-12-13 13:46

I have data as a list of floats and I want to plot it as a histogram. Hist() function does the job perfectly for plotting the absolute histogram. However, I cannot figure ou

相关标签:
2条回答
  • 2020-12-13 14:15

    Or you can use set_major_formatter to adjust the scale of the y-axis, as follows:

    from matplotlib import ticker as tick
    
    def adjust_y_axis(x, pos):
        return x / (len(mydata) * 1.0)
    
    ax.yaxis.set_major_formatter(tick.FuncFormatter(adjust_y_axis))
    

    just call adjust_y_axis as above before plt.show().

    0 讨论(0)
  • 2020-12-13 14:23

    Because normed option of hist returns the density of points, e.g dN/dx

    What you need is something like that:

     # assuming that mydata is an numpy array
     ax.hist(mydata, weights=np.zeros_like(mydata) + 1. / mydata.size)
     # this will give you fractions
    
    0 讨论(0)
提交回复
热议问题