我想知道是否存在使用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()
答案 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()
如果您希望将其绘制为一行,只需直接使用numpy.histogram
(这就是plt.hist
正在使用的内容)。或者,您可以使用plt.hist
返回的值。 counts
和bins
是np.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()
答案 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')