绘制同一细胞中的图

时间:2017-09-11 14:38:14

标签: 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

2 个答案:

答案 0 :(得分:2)

使用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

答案 1 :(得分:0)

您可以使用subplot(nrows, ncols, plot_number),其中使用nrows和ncols将图形概念拆分为nrows * ncols子轴,plot_number用于标识此函数在名义网格中创建的特定子图

在您的情况下,在每个绘图函数之前编写plt.subplot(1,2,1)plt.subplot(1, 2, 2)以指定绘制1行x 2 col网格。第三个参数是您希望绘制的子图。请参阅下面的代码修改:

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