Riccardo
Riccardo

Reputation: 299

matplotlib: plt.show() shows three figures

I'm using matplotlib to plot two graphs coming from some analysis on a set of emails. Here is the script that should show two figures and instead it actually show three. How can I solve this bug?

# Show the distribution of emails over the years
ax = emails_df.groupby(emails_df['Date'].dt.year)['content'].count().plot()
ax.set_xlabel('Year', fontsize=18)
ax.set_ylabel('N emails', fontsize=18)

f1 = plt.figure()

# Show the distribution of emails over a week
ax = emails_df.groupby(emails_df['Date'].dt.dayofweek)['content'].count().plot()
ax.set_xlabel('Day of week', fontsize=18)
ax.set_ylabel('N emails', fontsize=18)

f2 = plt.figure()

plt.show()

Upvotes: 0

Views: 273

Answers (1)

Moses Koledoye
Moses Koledoye

Reputation: 78556

The figure calls should come before the plots, not after. With your code, you'll have a blank figure with the last plt.figure call:

# figure 1
ax = emails_df.groupby(emails_df['Date'].dt.year)['content'].count().plot()
...

# figure 2
f1 = plt.figure()
...

# figure 3
f2 = plt.figure()

plt.show()

Fix this by moving the plt.figure calls before the preceding plot call.

Upvotes: 1

Related Questions