如何绘制尖顶图?

Fac*_*res 2 python statistics plot matplotlib

我想知道是否有一种方法可以在 Python 中使用 matplotlib 绘制直方图和尖顶图。

我有以下用于绘制直方图的内容

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

但我不知道 matplotlib 是否有绘制尖顶图的好方法。

这就是我正在做的:

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()
Run Code Online (Sandbox Code Playgroud)

Joe*_*ton 5

ogive您所说的只是累积直方图吗?如果是这样,只需传递cumulative=Trueplt.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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果你想让它画成一条线,numpy.histogram直接使用就可以了(就是这么plt.hist用的)。或者,您可以使用返回的值plt.histcounts以及会返回bins什么np.histogramplt.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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述