用Python将直方图转换为曲线

3
我有这段代码。
df1 = df['T1'].values

df1 = df1 [~np.isnan(df1 )].tolist()

plt.hist(df1 , bins='auto', range=(0,100))
plt.show()

这给了我这个图表

enter image description here

和这段代码

df2 = df['T2'].values

df2 = df2 [~np.isnan(df2 )].tolist()

plt.hist(df2 , bins='auto', range=(0,100))
plt.show()

这给了我这个

enter image description here

有没有办法将直方图转换为曲线,然后将它们组合在一起?
类似于这样:

enter image description here


直方图返回值 data = plt.hist(...),您可以使用 plt.plot(data[0]) - furas
2个回答

7
也许你想像这样绘制步骤
import numpy as np
import matplotlib.pyplot as plt

d1 = np.random.rayleigh(30, size=9663)
d2 = np.random.rayleigh(46, size=8083)

plt.hist(d1 , bins=np.arange(100), histtype="step")
plt.hist(d2 , bins=np.arange(100), histtype="step")
plt.show()

enter image description here


3
你可以使用 np.histogram
import numpy as np
from matplotlib import pyplot as plt

fig, ax = plt.subplots()

chisq = np.random.chisquare(3, 1000)
norm = np.random.normal(10, 4, 1000)

chisq_counts, chisq_bins = np.histogram(chisq, 50)
norm_counts, norm_bins = np.histogram(norm, 50)

ax.plot(chisq_bins[:-1], chisq_counts)
ax.plot(norm_bins[:-1], norm_counts)

plt.show()

对于你的数据这个具体情况,由于存在异常值,我们需要在绘图之前进行剪切

clipped_df1 = np.clip(df1, 0, 100)
clipped_df2 = np.clip(df2, 0, 100)

# continue plotting

enter image description here


获取到以下错误:Out[77]: [<matplotlib.lines.Line2D at 0x1b9ea4e0>] - asmgx
@asmgx 这并不是错误;你可能需要在此之后调用 plt.show() - gmds
谢谢,现在它可以用于您发布的示例,但不能用于我的数据。我得到的图形看起来像这样 https://i.stack.imgur.com/Ng416.png - asmgx
@asmgx 的原因是您的数据中存在一些离群值,这可能是您传递了 range=(0, 100) 的原因。我将编辑我的代码来纠正这个问题。 - gmds

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接