如何使用Python中的Matplotlib和数据列表绘制直方图?

时间:2021-07-28 23:44:29

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

我试图使用matplotlib.hist()函数绘制直方图,但我不知道该怎么做。

I have a list

我有一份清单

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]

and a list of names(strings).

和名单(字符串)列表。

How do I make the probability as my y-value of each bar and names as x-values?

如何将概率作为每个条形的y值和名称作为x值?

3 个解决方案

#1


48  

If you want a histogram, you don't need to attach any 'names' to x-values, as on x-axis you would have bins:

如果你想要一个直方图,你不需要将任何“名称”附加到x值,就像在x轴上你会有二进制文件:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
x = np.random.normal(size = 1000)
plt.hist(x, normed=True, bins=30)
plt.ylabel('Probability');

如何使用Python中的Matplotlib和数据列表绘制直方图?

However, if you have limited number of data points, and you want a bar plot, then you may attach labels to x-axis:

但是,如果数据点数量有限,并且需要条形图,则可以将标签附加到x轴:

x = np.arange(3)
plt.bar(x, height= [1,2,3])
plt.xticks(x+.5, ['a','b','c']);

如何使用Python中的Matplotlib和数据列表绘制直方图?

Let me know if this solves your problem.

如果这可以解决您的问题,请告诉我。

#2


4  

If you haven't installed matplotlib yet just try the command.

如果你还没有安装matplotlib,那就试试吧。

> pip install matplotlib

Library import

import matplotlib.pyplot as plot

The histogram data:

plot.hist(weightList,density=1, bins=20) 
plot.axis([50, 110, 0, 0.06]) 
#axis([xmin,xmax,ymin,ymax])
plot.xlabel('Weight')
plot.ylabel('Probability')

Display histogram

plot.show()

And the output is like :

如何使用Python中的Matplotlib和数据列表绘制直方图?

#3


1  

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:

这是一种非常圆润的方式,但是如果你想要制作直方图,你已经知道了bin值但没有源数据,你可以使用np.random.randint函数来生成正确数量的值在每个bin的范围内,用于绘制hist函数,例如:

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:

对于标签,您可以将x刻度与bin对齐以获得如下内容:

#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.. ])

#1


48  

If you want a histogram, you don't need to attach any 'names' to x-values, as on x-axis you would have bins:

如果你想要一个直方图,你不需要将任何“名称”附加到x值,就像在x轴上你会有二进制文件:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
x = np.random.normal(size = 1000)
plt.hist(x, normed=True, bins=30)
plt.ylabel('Probability');

如何使用Python中的Matplotlib和数据列表绘制直方图?

However, if you have limited number of data points, and you want a bar plot, then you may attach labels to x-axis:

但是,如果数据点数量有限,并且需要条形图,则可以将标签附加到x轴:

x = np.arange(3)
plt.bar(x, height= [1,2,3])
plt.xticks(x+.5, ['a','b','c']);

如何使用Python中的Matplotlib和数据列表绘制直方图?

Let me know if this solves your problem.

如果这可以解决您的问题,请告诉我。

#2


4  

If you haven't installed matplotlib yet just try the command.

如果你还没有安装matplotlib,那就试试吧。

> pip install matplotlib

Library import

import matplotlib.pyplot as plot

The histogram data:

plot.hist(weightList,density=1, bins=20) 
plot.axis([50, 110, 0, 0.06]) 
#axis([xmin,xmax,ymin,ymax])
plot.xlabel('Weight')
plot.ylabel('Probability')

Display histogram

plot.show()

And the output is like :

如何使用Python中的Matplotlib和数据列表绘制直方图?

#3


1  

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:

这是一种非常圆润的方式,但是如果你想要制作直方图,你已经知道了bin值但没有源数据,你可以使用np.random.randint函数来生成正确数量的值在每个bin的范围内,用于绘制hist函数,例如:

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:

对于标签,您可以将x刻度与bin对齐以获得如下内容:

#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.. ])