在Python中将直方图转换为曲线
我有这个密码在Python中将直方图转换为曲线,python,matplotlib,Python,Matplotlib,我有这个密码 df1 = df['T1'].values df1 = df1 [~np.isnan(df1 )].tolist() plt.hist(df1 , bins='auto', range=(0,100)) plt.show() 这给了我这个图表 这个密码呢 df2 = df['T2'].values df2 = df2 [~np.isnan(df2 )].tolist() plt.hist(df2 , bins='auto', range=(0,100)) plt.sho
df1 = df['T1'].values
df1 = df1 [~np.isnan(df1 )].tolist()
plt.hist(df1 , bins='auto', range=(0,100))
plt.show()
这给了我这个图表
这个密码呢
df2 = df['T2'].values
df2 = df2 [~np.isnan(df2 )].tolist()
plt.hist(df2 , bins='auto', range=(0,100))
plt.show()
这给了我这个
有没有办法把直方图转换成曲线,然后把它们组合在一起
像这样的
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()
您可能希望绘制这样的步骤
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()
您可以使用
np.直方图
:
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
直方图返回值
data=plt.hist(…)
您可以执行plt.plot(data[0])
将此错误清除[77]:[@asmgx这不是一个错误;您可能只需要调用plt.show()
之后。谢谢,现在它适用于您发布的示例,但不适用于我的数据。我得到的图形类似于@asmgx。原因是您的数据中存在一些异常值,这可能就是您通过范围=(0,100)
的原因。我将编辑我的代码以纠正这一点。