1

I know how to create matplotlib plots side by side, using plt.subplots() and the axes variable . But I cannot find how to do this when the plotting functions come from different python packages, let's say seaborn and scipy.stats.

Can anyone help?

import seaborn as sns
import matplotlib.pyplot as plt
import scipy.stats as stats
from scipy.stats import norm


train = [1,2,2,3,3,3,4,4,4,5,6]*2

#Distribution from seaborn
sns.distplot(train, fit=norm);
plt.ylabel('Frequency')

#QQ-plot plot from stats
plt.figure()
stats.probplot(train, plot=plt)
plt.show()

This is the output of the code (not side by side):

Output

Cheers,
Ricardo

Helios
  • 675
  • 4
  • 13
Ricardo Guerreiro
  • 497
  • 1
  • 4
  • 17
  • 1
    Does this answer your question? [How do I plot two countplot graphs side by side in seaborn?](https://stackoverflow.com/questions/43131274/how-do-i-plot-two-countplot-graphs-side-by-side-in-seaborn) – Helios Apr 08 '20 at 09:20
  • 2
    You arent plotting on the same figure in your question, the program is just plotting one figure after another (for example if you saved them you would not be saving into one figure). seaborn/scipy.stats plots work as an extension package to mpl. Also please include your imports; stats is not a top level package – Helios Apr 08 '20 at 09:26
  • Hi, thanks for the recommendation. It would maybe work with other functions, but this stats.probplot function (from scipy, I put the imports now) doesn't accept axes as a parameter. – Ricardo Guerreiro Apr 08 '20 at 10:37

2 Answers2

2

It's confusing because stats.probplot() uses different syntax (plot= vs. ax=...; see the docs for sns.distplot and stats.probplot), but this should work.

import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats

train = [1,2,2,3,3,3,4,4,4,5,6]*2

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

#Distribution from seaborn
sns.distplot(train, ax=ax[0]);

#QQ-plot plot from stats
stats.probplot(train, plot=ax[1])

enter image description here

Peter Prescott
  • 768
  • 8
  • 16
1

From the comment - both are quite thin wrappers and rely on/forward a lot on to mpl

as not identical to linked question;

import seaborn as sns
import matplotlib.pyplot as plt
import scipy.stats as stats
from scipy.stats import norm

train = [1,2,2,3,3,3,4,4,4,5,6]*2

fig, ax = plt.subplots(1,2)
sns.distplot(train, fit=norm, ax=ax[0])
ax[0].set_ylabel('Frequency')
stats.probplot(train, plot=ax[1])
plt.show()
Helios
  • 675
  • 4
  • 13