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

前端 未结 5 765
醉梦人生
醉梦人生 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:16

    Though the question appears to be demanding plotting a histogram using matplotlib.hist() function, it can arguably be not done using the same as the latter part of the question demands to use the given probabilities as the y-values of bars and given names(strings) as the x-values.

    I'm assuming a sample list of names corresponding to given probabilities to draw the plot. A simple bar plot serves the purpose here for the given problem. The following code can be used:

    import matplotlib.pyplot as plt
    probability = [0.3602150537634409, 0.42028985507246375, 
      0.373117033603708, 0.36813186813186816, 0.32517482517482516, 
      0.4175257731958763, 0.41025641025641024, 0.39408866995073893, 
      0.4143222506393862, 0.34, 0.391025641025641, 0.3130841121495327, 
      0.35398230088495575]
    names = ['name1', 'name2', 'name3', 'name4', 'name5', 'name6', 'name7', 'name8', 'name9',
    'name10', 'name11', 'name12', 'name13'] #sample names
    plt.bar(names, probability)
    plt.xticks(names)
    plt.yticks(probability) #This may be included or excluded as per need
    plt.xlabel('Names')
    plt.ylabel('Probability')
    

提交回复
热议问题