0

I want to put Sea born graphs into sub-plots using the following code. Can some body suggest the right approach?

plt.figure(figsize=(15,5)) 
plt.subplot(1,2,1)
sns.lmplot(data= df, x='Attack', y= 'Defense') 
plt.subplot(1,2,2) 
sns.jointplot(data= df, x='Attack', y= 'Defense', kind='scatter',color='purple') 
plt.show()
JohanC
  • 71,591
  • 8
  • 33
  • 66

1 Answers1

4

At the moment you can not do it in a simple way. Some plots in seaborn are defined at the figure-level (e.g. lmplot, factorplot, jointplot etc.) and they do not accept the axis argument.

If you really want to create subplots you can follow this answer here which includes a support class (SeabornFig2Grid) to do exactly what you need. Here an example of that class in use:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import seaborn as sns; sns.set()


df = pd.DataFrame(dict(
    Attack = np.random.random(20),
    Defense = np.random.random(20)))

g0 = sns.lmplot(data= df, x='Attack', y= 'Defense') 
g1 = sns.jointplot(data= df, x='Attack', y= 'Defense', kind='scatter',color='purple') 
fig = plt.figure(figsize=(8,4))
gs = gridspec.GridSpec(1, 2)

mg0 = SeabornFig2Grid(g0, fig, gs[0])
mg1 = SeabornFig2Grid(g1, fig, gs[1])

gs.tight_layout(fig)

enter image description here

Andrea
  • 2,932
  • 11
  • 23