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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33203645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:00:20  来源:igfitidea点击:

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

pythonmatplotlibvisualizationdata-visualization

提问by DataVizGuys

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 值?

回答by Sergey Bushmanov

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

如果你想要一个直方图,你不需要将任何“名称”附加到 x 值,因为在 x 轴上你会有数据箱:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
np.random.seed(42)
x = np.random.normal(size=1000)
plt.hist(x, density=True, bins=30)  # `density=False` would make counts
plt.ylabel('Probability')
plt.xlabel('Data');

enter image description here

在此处输入图片说明

You can make your histogram a bit fancier with PDFline, titles, and legend:

您可以使用PDF线条、标题和图例使直方图更加美观:

import scipy.stats as st
plt.hist(x, density=True, bins=30, label="Data")
mn, mx = plt.xlim()
plt.xlim(mn, mx)
kde_xs = np.linspace(mn, mx, 301)
kde = st.gaussian_kde(x)
plt.plot(kde_xs, kde.pdf(kde_xs), label="PDF")
plt.legend(loc="upper left")
plt.ylabel('Probability')
plt.xlabel('Data')
plt.title("Histogram");

enter image description here

在此处输入图片说明

However, if you have limited number of data points, like in OP, a bar plot would make more sense to represent your data (then you may attach labels to x-axis):

但是,如果您的数据点数量有限,例如在 OP 中,条形图更能代表您的数据(然后您可以将标签附加到 x 轴):

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

enter image description here

在此处输入图片说明

回答by Connor Wilmers

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.randintfunction 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 刻度与垃圾箱对齐以获得如下所示的内容:

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

回答by Niraj

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 :

输出如下:

enter image description here

在此处输入图片说明

回答by Shayan Shafiq

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.

尽管该问题似乎要求使用matplotlib.hist()函数绘制直方图,但可以说不能使用与问题的后半部分要求使用给定概率作为条形的 y 值和给定名称(字符串)作为x 值。

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')