Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 直方图矩阵库_Python_Numpy_Matplotlib_Scipy_Histogram - Fatal编程技术网

Python 直方图矩阵库

Python 直方图矩阵库,python,numpy,matplotlib,scipy,histogram,Python,Numpy,Matplotlib,Scipy,Histogram,所以我有一个小问题。我有一个scipy中的数据集,它已经是直方图格式,所以我有箱子的中心和每个箱子的事件数。我现在如何绘制柱状图。我试着去做 bins, n=hist() 但它不喜欢这样。有什么建议吗 import matplotlib.pyplot as plt import numpy as np mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) hist, bins = np.histogram(x, bins=50

所以我有一个小问题。我有一个scipy中的数据集,它已经是直方图格式,所以我有箱子的中心和每个箱子的事件数。我现在如何绘制柱状图。我试着去做

bins, n=hist()
但它不喜欢这样。有什么建议吗

import matplotlib.pyplot as plt
import numpy as np

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
hist, bins = np.histogram(x, bins=50)
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width)
plt.show()

面向对象的接口也很简单:

fig, ax = plt.subplots()
ax.bar(center, hist, align='center', width=width)
fig.savefig("1.png")

如果您使用的是自定义(非常量)箱子,您可以使用
np.diff
传递计算宽度,将宽度传递到
ax.bar
并使用
ax.set_xticks
标记箱子边缘:

import matplotlib.pyplot as plt
import numpy as np

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
bins = [0, 40, 60, 75, 90, 110, 125, 140, 160, 200]
hist, bins = np.histogram(x, bins=bins)
width = np.diff(bins)
center = (bins[:-1] + bins[1:]) / 2

fig, ax = plt.subplots(figsize=(8,3))
ax.bar(center, hist, align='center', width=width)
ax.set_xticks(bins)
fig.savefig("/tmp/out.png")

plt.show()

如果不需要条形图,可以这样绘制:

import numpy as np
import matplotlib.pyplot as plt

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)

bins, edges = np.histogram(x, 50, normed=1)
left,right = edges[:-1],edges[1:]
X = np.array([left,right]).T.flatten()
Y = np.array([bins,bins]).T.flatten()

plt.plot(X,Y)
plt.show()
如果您愿意使用:


我知道这并不能回答您的问题,但当我搜索直方图的matplotlib解决方案时,我总是在这一页结束,因为简单的
直方图演示已从matplotlib示例库页面中删除


这里有一个解决方案,它不需要导入
numpy
。我只导入numpy以生成要打印的数据
x
。它依赖于函数,而不是@unutbu中的函数

import numpy as np
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)

import matplotlib.pyplot as plt
plt.hist(x, bins=50)
plt.savefig('hist.png')


还可以查看和。

这可能对某些人有用

Numpy的直方图函数返回每个箱子的边缘,而不是箱子的值。这对于浮点数是有意义的,浮点数可以位于一个区间内,但在处理离散值或整数(0、1、2等)时可能不是期望的结果。特别是,从np.histogram返回的箱子长度不等于计数/密度的长度

为了解决这个问题,我使用np.digitalize对输入进行量化,并计算每个箱子的计数分数。您可以轻松地编辑以获得整数计数

def compute_PMF(data):
    import numpy as np
    from collections import Counter
    _, bins = np.histogram(data, bins='auto', range=(data.min(), data.max()), density=False)
    h = Counter(np.digitize(data,bins) - 1)
    weights = np.asarray(list(h.values())) 
    weights = weights / weights.sum()
    values = np.asarray(list(h.keys()))
    return weights, values
####
参考文献:

[1]

[2]

我刚刚意识到,当您已经有了一个
np.直方图时,文档明确说明了要做什么

counts, bins = np.histogram(data)
plt.hist(bins[:-1], bins, weights=counts)

这里重要的一点是,您的计数只是权重。如果你这样做,你不再需要条形函数了

如果你打算建议使用熊猫
,你可能应该包括一个到他们网站的链接和一个更通俗的例子来解释发生了什么。你也可以使用ax.step
。有没有办法将箱子边缘传递到条形图的x轴?@cmcdragokai:
plt.bar
width
参数可以接受类似数组的对象(而不是标量)。因此,您可以使用
width=np.diff(bins)
而不是
width=0.7*(bins[1]-bins[0])
。但是
width
设置本身只会正确设置条的宽度?我说的是x轴标签(也就是说,我希望看到实际的箱子边缘是x轴上的标签)。它应该类似于
plt.hist
的工作方式。@cmcdragokai:您可以使用
ax.set\u xticks
来设置xlabel。我在上面添加了一个示例来说明我的意思。“这里有一个解决方案,它不需要numpy”--第一行代码导入numpy:)@martinr。这只是生成要绘制的数据。见第4-6行。不使用numpy。
counts, bins = np.histogram(data)
plt.hist(bins[:-1], bins, weights=counts)