2

I was plotting some scatter plot of mpg dataset from the seaborn library. I was wondering if it is possible to plot the odd number of subplots in python?

import pandas as pd
import seaborn as sns
df = sns.load_dataset('mpg')
df.groupby('origin').plot.scatter(x='cylinders',y='mpg',subplots=True,layout=(2,2))

This gives 4,1 plots not 2,2 plots:
enter image description here

Is there a way of obtaining 3 subplolts for usa,europe and japan with label names like shown below in more easier way?

This is a long way of doing it:

import pandas as pd
import seaborn as sns
df = sns.load_dataset('mpg')
g = df.groupby('origin')

fig, axes = plt.subplots(2,2,figsize=(8, 4))
for i, (g_name, g_data) in enumerate(g): # axes.flat also works
    ax = g_data.plot.scatter(x='cylinders', y='mpg', ax=axes.flatten()[i], title=g_name)

plt.tight_layout()
fig.delaxes(axes[1][1])
BhishanPoudel
  • 15,974
  • 21
  • 108
  • 169
  • Possible duplicate of [matplotlib add_subplot odd number of plots](https://stackoverflow.com/questions/41025187/matplotlib-add-subplot-odd-number-of-plots) – cfort Apr 18 '19 at 13:51
  • Possible duplicate of [Delete a subplot](https://stackoverflow.com/questions/14694501/delete-a-subplot) – DavidG Apr 18 '19 at 14:39

1 Answers1

2

Try delaxes:

fig.delaxes(axes[1][1])
dubbbdan
  • 2,650
  • 1
  • 25
  • 43