How to plot a histogram using Matplotlib in Python with a list of data?

前端 未结 5 781
醉梦人生
醉梦人生 2020-12-04 09:06

I am trying to plot a histogram using the matplotlib.hist() function but I am not sure how to do it.

I have a list

probability = [0.360         


        
5条回答
  •  悲&欢浪女
    2020-12-04 09:35

    This is a very round-about way of doing it but if you want to make a histogram where you already know the bin values but dont have the source data, you can use the np.random.randint function to generate the correct number of values within the range of each bin for the hist function to graph, for example:

    import numpy as np
    import matplotlib.pyplot as plt
    
    data = [np.random.randint(0, 9, *desired y value*), np.random.randint(10, 19, *desired y value*), etc..]
    plt.hist(data, histtype='stepfilled', bins=[0, 10, etc..])
    

    as for labels you can align x ticks with bins to get something like this:

    #The following will align labels to the center of each bar with bin intervals of 10
    plt.xticks([5, 15, etc.. ], ['Label 1', 'Label 2', etc.. ])
    

提交回复
热议问题