Reputation: 2495
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
defaults.grade
is a list ['A','B','C','D'...]
defaults.grade_pct_of_total
is a series of floatsUpvotes: 1
Views: 4587
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