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

标签 python python-3.x matplotlib jupyter

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

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

: enter image description here

如何并排呈现这些分布?

我尝试过使用子图:

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

但这并不正确:

enter image description here

如何并排渲染两个或更多图?

更新:

按照 @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()

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

enter image description here

最佳答案

使用方法fig, axs = plt.subplots(1,2)是正确的。它会给你一个数字 fig和轴数组 axs .
接下来您需要做的是明确使用这些轴。而不是plt.plot你可以打电话 axs[0].plot()绘制到第一个轴和 axs[1].plot()绘制到第二个轴。 .hist 也一样打电话。

最后,您还需要单独为每个子图设置标题,axs[0].set_title(title)而不是plt.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()

enter image description here

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

相关文章:

python - 带有 opencv 的 matplotlib 给出具有相同像素值的不同图像

python - 显示处理后的视频帧(一帧一帧)Python

javascript - 我如何从客户端(html)拍照并将其保存到服务器端(Python)

python - 将数据框的所有数字列转换为绝对值

python - Python 中的装饰器和临时名称绑定(bind)

python - 如何编码日志文件?

python - 安装 django 项目时出错 AttrributeError : module 'collections' has no attribute 'Iterator'

python - 将变量(非模型字段)传递给 Django Rest 框架中的序列化器

python-3.x - 使用 xarray 计算月平均值

python - 如何使用 matplotlibrc 文件在一个轴上添加网格线