0

Multi plot-layout troubles

Trying to plot 3 graphs next to each other, by using mathplotlib for the grid-layout and Seaborn to plot the graphs. The problem is i get to many plots, 3 perfects once + 3 empty coordinate systems.

What I tried

The problem comes from mixing the seaborn plot-lib with the matplotlib.

  1. Test: is what is seen in the code example and the picture "My result"
  2. Test: if "ax=axarr[x]" is removed from the seaborn plot, the results are switched, so the three top graphs are empty and last three is filled, as expected.
fig, axarr = plt.subplots(1, 3, figsize=(20, 6))
sns.catplot(x='weekday', kind='count', palette="ch:.25", data=df_total, ax=axarr[2])
sns.catplot(x='month', kind='count', palette="ch:.25", data=df_total, ax=axarr[1])
sns.catplot(x='year', kind='count', palette="ch:.25", data=df_total, ax=axarr[0])

My results:

My result

What I want to achieve:

Trying to achieve

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • This comes up every week or so, you can search for older questions and answers. In short: catplot does not take an `ax` argument, because it creates its own figure. (Though I would be interested to know *why* so many people try to use catplot with an `ax` argument?) – ImportanceOfBeingErnest Jul 24 '19 at 19:28

1 Answers1

0

Use countplot rather than catplot:

n = 365*10
df_total = pd.DataFrame({'date':pd.date_range(start='2009/1/1', periods=n, freq='D'),
                        'x':np.random.randn(n)})

df_total = df_total.sample(500)
df_total['weekday'] = df_total['date'].dt.weekday
df_total['month'] = df_total['date'].dt.month
df_total['year'] = df_total['date'].dt.year

#fig, axarr = plt.subplots(1, 3, figsize=(20, 6))
#sns.catplot(x='weekday', kind='count', palette="ch:.25", data=df_total, ax=axarr[2])
#sns.catplot(x='month', kind='count', palette="ch:.25", data=df_total, ax=axarr[1])
#sns.catplot(x='year', kind='count', palette="ch:.25", data=df_total, ax=axarr[0])

fig, axarr = plt.subplots(1, 3, figsize=(12, 4))
sns.countplot(x='weekday', palette="ch:.25", data=df_total, ax=axarr[2])
sns.countplot(x='month', palette="ch:.25", data=df_total, ax=axarr[1])
sns.countplot(x='year', palette="ch:.25", data=df_total, ax=axarr[0])
plt.show()

enter image description here

Brendan
  • 3,901
  • 15
  • 23