Reputation: 53826
This code renders two normal distributions :
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()
How to renders these distributions side by side ?
I've tried using subplots :
fig, axs = plt.subplots(1,2)
So previous code becomes :
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()
But this is not correct as is rendered :
How to render two plots or more side by side ?
Update :
Using code as per @Varun Balupuri answer :
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()
renders plot's side by side but are overlapping , line is missing from left side chart , histogram is missing from right side chart :
Upvotes: 2
Views: 536
Reputation: 339300
The approach to use fig, axs = plt.subplots(1,2)
is correct. It will give you a figure fig
and an array of axes axs
.
What you need to do next is to use those axes explicitely. Instead of plt.plot
you'd call axs[0].plot()
to plot to the first axes and axs[1].plot()
to plot to the second axes. The same for the .hist
call.
Finally,you'd also want to set the title to each subplot individually, axs[0].set_title(title)
instead of plt.title(title)
.
Additionally the code below corrects the data limits for the pdf to use the axes limits of the subplots.
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()
Upvotes: 2
Reputation: 363
You can use subplot(nrows, ncols, plot_number), where nrows and ncols are used to notionally split the figure into nrows * ncols sub-axes, and plot_number is used to identify the particular subplot that this function is to create within the notional grid.
In your case, writing plt.subplot(1,2,1)
and plt.subplot(1, 2, 2)
prior to each plotting function to specify to plot a 1 row x 2 col grid. The third argument is which subplot you wish to plot in. See the below modification of your code:
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()
Upvotes: 0