如何策划一个ogive?

时间:2013-11-15 21:39:53

标签: python matplotlib plot statistics

我想知道是否存在使用Python中的matplotlib绘制直方图和ogive的方法。

我有以下用于绘制直方图

a = np.array(values)
plt.hist(a, 32, normed=0, facecolor='blue', alpha = 0.25)
plt.show()

但我不知道matplotlib是否有一个很好的方式来绘制一个ogive。

这就是我正在做的事情:

a = np.array(values)
bins = np.arange(int(min), int(max) + 2)
histogram = np.histogram(a, bins = bins, normed = True)
v = []
s = 0.0
for e in histogram[0]:
    s = s + e
    v.append(s)
v[0] = histogram[0][0]
plt.plot(v)
plt.show()

2 个答案:

答案 0 :(得分:3)

ogive你的意思是累积直方图吗?如果是,请将cumulative=True传递给plt.hist

例如:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.normal(0, 1, 1000)

fig, (ax1, ax2) = plt.subplots(nrows=2)
ax1.hist(data)
ax2.hist(data, cumulative=True)
plt.show()

enter image description here

如果您希望将其绘制为一行,只需直接使用numpy.histogram(这就是plt.hist正在使用的内容)。或者,您可以使用plt.hist返回的值。 countsbinsnp.histogram将返回的内容; plt.hist也只返回绘制的补丁。

例如:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.normal(0, 1, 1000)

fig, ax = plt.subplots()
counts, bins, patches = plt.hist(data)

bin_centers = np.mean(zip(bins[:-1], bins[1:]), axis=1)
ax.plot(bin_centers, counts.cumsum(), 'ro-')

plt.show()

enter image description here

答案 1 :(得分:1)

目前形式的问题非常模糊。 x和y比例是相似还是不同?假设x等级相同,它应该非常简单。请注意,由于您尚未提供任何数据,我尚未测试下面的代码

import numpy as np
import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()

ax1.hist(values, 32, normed=0, facecolor='blue', alpha=0.25)
ax2.plot(x_ogive, y_ogive, marker='none', linestyle='-', color='black')

ax1.set_xlabel('X-data')
ax1.set_ylabel('Counts')
ax2.set_ylabel('Ogive Surface')

fig.savefig('OgiveAndHist.png')