【问题标题】:plot norm curve over subplotted histograms在子图直方图上绘制范数曲线
【发布时间】:2020-09-25 20:11:37
【问题描述】:

我想绘制多个包含直方图的子图。此外,我想绘制一条曲线,显示每个子图的正态分布。虽然我在这个论坛上找到了关于如何在单个图(直方图)上绘制正态曲线的不同答案,但我正在努力用子图实现相同的效果。我尝试了以下方法:

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()  

现在我的问题是,正态曲线对于第二个情节总是最优的,但不是第一个情节。这是因为 xmin 和 xmax,但是我不知道如何将这两个命令单独放入子图中。有人对这个有经验么?我一直在尝试整个下午

任何帮助都非常感谢,在此先感谢!

【问题讨论】:

  • 使用ax1.get_xticks()ax2.get_xticks() 代替plt.xticks()。也许ax1.get_xlim(),因为它会直接获得限制,默认以正确的顺序。 ax1.margins(x=0) 会将新的限制再次设置为曲线的限制,因此它不会“悬空”。 This tutorial 可能会有所帮助。

标签: python matplotlib histogram


【解决方案1】:

您可以使用axes 代替元组。然后您可以使用sca 单独设置每个轴。如果这是您需要的,请参见下文。

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()  

【讨论】:

  • 嘿 mgmussi,非常感谢您的回答。当我尝试添加更多子图(axes[2]、axes[3])时,我收到错误消息“具有多个元素的数组的真值不明确”。你知道如何添加更多的子图吗?
  • @Sanoj 当行数和列数都大于1时,需要axes[row_index][col_index]。请不要使用plt.xticks(),而是使用axes[...][...].get_xticks()。使用plt.sca()设置当前斧头可以,但是很混乱,容易出错。
  • @Sanoj 你可以按照@JohanC 的建议去做,也可以使用axes[line, column] 来引用坐标轴我的情况是你的子图中有多个“维度”
  • 最后一个问题:如何解释 y 轴?它太低了,不可能是百分比,也不是那个 bin 中的值的数量......
  • 我不太确定,但可能是百分比但除以 100。如果将所有条形加起来,它应该都加到 100%。
猜你喜欢
  • 2018-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-19
  • 1970-01-01
相关资源
最近更新 更多