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])
2

1 Answer 1

2

Try delaxes:

fig.delaxes(axes[1][1])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.