带曲线的直方图,表示直方图趋势

2024-09-28 18:56:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经绘制了柱状图,现在我想要一条曲线,它将代表柱状图的趋势。我希望我的直方图分类是对数的(正如我在下面的代码中所做的那样;质量变量是预定义的变量,范围为10^43-10^45克)

我已经寻找了很多代码,但都不能适合我的情况(也尝试过修改)。你知道我怎么画这条曲线吗?实际上,我只是想修改我的代码,使它也包括在直方图上方绘制这条曲线

谢谢

莎乐美

See the attached image

import matplotlib.pyplot as plt
import numpy as np
x=Mass
hist, bins = np.histogram(x, bins=10)
logbins = np.logspace(np.log10(bins[0]),np.log10(bins[-1]),len(bins))

n, bins, patches = plt.hist(x=Mass, bins=logbins, color='#0504aa', alpha=0.8, rwidth=0.85) 
plt.xscale('log')
plt.xlabel('Mass $(g)$ ')
plt.ylabel('Number of halos')
plt.show()


Tags: 代码importasnp绘制plt代表直方图