GPB
GPB

Reputation: 2495

How to save pyplot figure

I am trying to save a pyplot figure to a .png file.

Problem is, the graphic displays on the screen, but the file is a blank page!

import matplotlib.pyplot as plt

# Pie plotting code from Matplotlib.org 
plt.figure(figsize=(12,9))
labels = defaults.grade
sizes = defaults.grade_pct_of_total
colors = ['yellowgreen', 'gold', 'lightskyblue', 
          'lightcoral','blue','red','orange']
explode = (0.1, 0.1, 0.1, 0.1,0.1,0.1,0.1)

plt.pie(sizes, explode=explode, labels=labels, colors=colors,
        autopct='%1.1f%%', shadow=True, startangle=90)

plt.title('Default contribution % \n\n')

# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')

plt.show()
plt.savefig('Default Contributions by Grade.pdf', bbox_inches='tight')

Note that

Upvotes: 1

Views: 4587

Answers (1)

Haochen Wu
Haochen Wu

Reputation: 1823

plt.show() will clear your canvas as well. Simply remove it or run plt.savefig before plt.show will fix your problem.

Upvotes: 4

Related Questions