[英]plot norm curve over subplotted histograms
I would like to plot multiple subplots containing histograms.我想绘制包含直方图的多个子图。 Additionally, I would like to plot a curve showing the normal distribution for each subplot.
此外,我想绘制一条曲线,显示每个子图的正态分布。 While I found different answers on this forum on how to plot a normal curve over a single plot (histogram), I am struggling to achieve the same with subplots.
虽然我在这个论坛上找到了关于如何在单个图(直方图)上绘制正态曲线的不同答案,但我正在努力用子图实现相同的目标。 I have tried the following:
我尝试了以下方法:
from scipy import stats
import numpy as np
import matplotlib.pylab as plt
fig, ((ax1, ax2)) = plt.subplots(1,2,figsize=(10,4))
# create some normal random noisy data
data1 = 50*np.random.rand() * np.random.normal(10, 10, 100) + 20
data2= 50*np.random.rand() * np.random.normal(10, 10, 100) + 50
# plot normed histogram
ax1.hist(data1, density=True)
# find minimum and maximum of xticks,
xt = plt.xticks()[0]
xmin, xmax = min(xt), max(xt)
lnspc = np.linspace(xmin, xmax, len(data1))
# lets try the normal distribution first
m1, s1 = stats.norm.fit(data1) # get mean and standard deviation
pdf_1 = stats.norm.pdf(lnspc, m1, s1) # now get theoretical values in our interval
ax1.plot(lnspc, pdf_1, label="Norm") # plot it
# plot second hist
ax2.hist(data2, density=True)
# find minimum and maximum of xticks
xt = plt.xticks()[0]
xmin, xmax = min(xt), max(xt)
lnspc = np.linspace(xmin, xmax, len(data2))
# lets try the normal distribution first
m2, s2 = stats.norm.fit(data2) # get mean and standard deviation
pdf_2 = stats.norm.pdf(lnspc, m2, s2) # now get theoretical values in our interval
ax2.plot(lnspc, pdf_2, label="Norm") # plot it
plt.show()
Now my problem is that the normal curve is always optimal for the second plot but not the first.现在我的问题是正态曲线对于第二个图总是最佳的,但不是第一个。 This is because of xmin and xmax, I however don't know how to fit these two commands invdividually in subplots.
这是因为 xmin 和 xmax,但是我不知道如何在子图中单独拟合这两个命令。 Does anyone have any experience with this?
有人对这个有经验么? I have been trying all afternoon
我整个下午都在努力
Any help is highly appreciated, thanks in advance!非常感谢任何帮助,提前致谢!
You can use axes
instead of a tuple.您可以使用
axes
而不是元组。 Then you can set each axis individually using sca
.然后您可以使用
sca
单独设置每个轴。 See below if that's what you needed.如果这是您需要的,请参见下文。
from scipy import stats
import numpy as np
import matplotlib.pylab as plt
# fig, ((ax1, ax2)) = plt.subplots(1,2,figsize=(10,4)) << INSTEAD OF THIS DO:
fig, axes = plt.subplots(nrows = 1, ncols = 2,figsize=(10,4))
# create some normal random noisy data
data1 = 50*np.random.rand() * np.random.normal(10, 10, 100) + 20
data2= 50*np.random.rand() * np.random.normal(10, 10, 100) + 50
plt.sca(axes[0]) #Refer to the first axis
# plot normed histogram
axes[0].hist(data1, density=True)
# find minimum and maximum of xticks,
xt = plt.xticks()[0]
xmin, xmax = min(xt), max(xt)
lnspc = np.linspace(xmin, xmax, len(data1))
# lets try the normal distribution first
m1, s1 = stats.norm.fit(data1) # get mean and standard deviation
pdf_1 = stats.norm.pdf(lnspc, m1, s1) # now get theoretical values in our interval
axes[0].plot(lnspc, pdf_1, label="Norm") # plot it
plt.sca(axes[1]) #Refer to the second axis
# plot second hist
axes[1].hist(data2, density=True)
# find minimum and maximum of xticks
xt = plt.xticks()[0]
xmin, xmax = min(xt), max(xt)
lnspc = np.linspace(xmin, xmax, len(data2))
# lets try the normal distribution first
m2, s2 = stats.norm.fit(data2) # get mean and standard deviation
pdf_2 = stats.norm.pdf(lnspc, m2, s2) # now get theoretical values in our interval
axes[1].plot(lnspc, pdf_2, label="Norm") # plot it
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.