0

I am trying to put 2 matplotlib figures side by side but each resulted from a groupby function (see code below, only show the second plot). I am aware that similar questions have been asked before but none are exactly like this one. I appreciate any help.

Image here

ax = df_train.groupby(['Role','Side'])['Result'].mean().plot(kind='bar', figsize=(5,6), fontsize=13)
ax.set_ylabel('Win Rate')
ax.set_title('Win Rate by Role and Side')


ax1 = df_train.groupby(['Role','Side'])['Result'].count().plot(kind='bar', figsize=(5,6), fontsize=13)
ax1.set_ylabel('# of games')
ax1.set_title('# of games by Role and Side')

plt.show()
Dat Nguyen
  • 13
  • 5
  • Does this answer your question? [How can I plot separate Pandas DataFrames as subplots?](https://stackoverflow.com/questions/22483588/how-can-i-plot-separate-pandas-dataframes-as-subplots) – Diziet Asahi May 10 '20 at 11:41
  • I found that link in my first search but it unfortunately doesn't work with the codes. The error is `'Series' object has no attribute 'subplots'` – Dat Nguyen May 11 '20 at 06:45

1 Answers1

0

As explained in other answers on SO, this is what you need to do:

fig, (ax1, ax2) = plt.subplots(1,2, figsize=(10,6))
df_train.groupby(['Role','Side'])['Result'].mean().plot(kind='bar', ax=ax1, fontsize=13)
ax1.set_ylabel('Win Rate')
ax1.set_title('Win Rate by Role and Side')

df_train.groupby(['Role','Side'])['Result'].count().plot(kind='bar', ax=ax2, fontsize=13)
ax2.set_ylabel('# of games')
ax2.set_title('# of games by Role and Side')

plt.show()

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75