如何保存pyplot数字

时间:2015-08-12 20:04:16

标签: python matplotlib

我正在尝试将pyplot数字保存到.png文件。

问题是,图形显示在屏幕上,但文件是空白页!

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')

请注意

  • defaults.grade是一个列表['A','B','C','D'...]
  • defaults.grade_pct_of_total是一系列花车

1 个答案:

答案 0 :(得分:2)

plt.show()也会清除你的画布。只需将其删除或运行plt.savefigplt.show即可解决问题。