此代码呈现两个正态分布:

from scipy.stats import norm
import matplotlib.pyplot as plt
import numpy as np

data = norm.rvs(10.0, 2.5, size=500)
mu, std = norm.fit(data)
plt.hist(data, bins=25, normed=True, alpha=0.6, color='g')
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, std)
plt.plot(x, p, 'k', linewidth=3)
title = "Fit results: mu = %.2f,  std = %.2f" % (mu, std)

plt.title(title)
fig = plt.gcf()
fig.set_size_inches(4, 3)

plt.show()

data = norm.rvs(10.0, 2.5, size=500)
mu, std = norm.fit(data)
plt.hist(data, bins=25, normed=True, alpha=0.6, color='g')
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, std)

plt.plot(x, p, 'k', linewidth=2)
title = "Fit results: mu = %.2f,  std = %.2f" % (mu, std)
plt.title(title)
fig = plt.gcf()
fig.set_size_inches(4, 3)

plt.show()



python - 在同一单元格中绘制图-LMLPHP

如何并排渲染这些分布?

我试过使用子图:

fig, axs = plt.subplots(1,2)


因此,先前的代码变为:

from scipy.stats import norm
import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(1,2)

data = norm.rvs(10.0, 2.5, size=500)
mu, std = norm.fit(data)
plt.hist(data, bins=25, normed=True, alpha=0.6, color='g')
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, std)
plt.plot(x, p, 'k', linewidth=3)
title = "Fit results: mu = %.2f,  std = %.2f" % (mu, std)

plt.title(title)
fig = plt.gcf()
fig.set_size_inches(4, 3)

plt.show()

data = norm.rvs(10.0, 2.5, size=500)
mu, std = norm.fit(data)
plt.hist(data, bins=25, normed=True, alpha=0.6, color='g')
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, std)

plt.plot(x, p, 'k', linewidth=2)
title = "Fit results: mu = %.2f,  std = %.2f" % (mu, std)
plt.title(title)
fig = plt.gcf()
fig.set_size_inches(4, 3)

plt.show()


但这不正确,如呈现:

python - 在同一单元格中绘制图-LMLPHP

如何并排绘制两个或更多图?

更新:

根据@Varun Balupuri答案使用代码:

from scipy.stats import norm
import matplotlib.pyplot as plt
import numpy as np

data = norm.rvs(10.0, 2.5, size=500)
mu, std = norm.fit(data)
plt.hist(data, bins=25, normed=True, alpha=0.6, color='g')
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, std)
plt.plot(x, p, 'k', linewidth=3)
title = "Fit results: mu = %.2f,  std = %.2f" % (mu, std)

plt.title(title)
fig = plt.gcf()
fig.set_size_inches(4, 3)

# plot in the first subplot
plt.subplot(1,2,1)

data = norm.rvs(10.0, 2.5, size=500)
mu, std = norm.fit(data)
plt.hist(data, bins=25, normed=True, alpha=0.6, color='g')


xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, std)

# plot in the second subplot
plt.subplot(1, 2, 2)

plt.plot(x, p, 'k', linewidth=2)
title = "Fit results: mu = %.2f,  std = %.2f" % (mu, std)
plt.title(title)
fig = plt.gcf()
fig.set_size_inches(4, 3)

plt.show()


并排渲染图,但重叠,左侧图表缺少线,右侧图表缺少直方图:

python - 在同一单元格中绘制图-LMLPHP

最佳答案

使用fig, axs = plt.subplots(1,2)的方法是正确的。它将为您提供一个图形fig和一个轴axs数组。
接下来,您需要明确地使用这些轴。代替plt.plot,您将调用axs[0].plot()绘制到第一个轴,而调用axs[1].plot()绘制到第二个轴。 .hist调用相同。

最后,您还希望为每个子图分别设置标题,而不是axs[0].set_title(title)

另外,以下代码更正了pdf的数据限制,以使用子图的轴限制。

from scipy.stats import norm
import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(1,2, figsize=(5,3))

# first subplot is axs[0]
data = norm.rvs(10.0, 2.5, size=500)
mu, std = norm.fit(data)
axs[0].hist(data, bins=25, normed=True, alpha=0.6, color='g')
xmin, xmax = axs[0].get_xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, std)
axs[0].plot(x, p, 'k', linewidth=2)
title = "Fit results:\n mu = %.2f,\n  std = %.2f" % (mu, std)
axs[0].set_title(title)

# second subplot is axs[1]
data = norm.rvs(10.0, 2.5, size=500)
mu, std = norm.fit(data)
axs[1].hist(data, bins=25, normed=True, alpha=0.6, color='g')
xmin, xmax = axs[1].get_xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, std)
axs[1].plot(x, p, 'k', linewidth=2)
title = "Fit results:\n mu = %.2f,\n  std = %.2f" % (mu, std)
axs[1].set_title(title)

plt.tight_layout()
plt.show()


python - 在同一单元格中绘制图-LMLPHP

关于python - 在同一单元格中绘制图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46158328/

10-12 05:29