Matplotlib:来自plt.savefig()和手动保存的不同视图

时间:2015-12-03 13:42:55

标签: python matplotlib plot pie-chart figure

如果我用plt.savefig('piechart.png')保存我的matplotlib图,我会在这里得到这个数字: 尺寸:800x600,99 DPI

piechart from plt.savefig()

如果我用plt.show()显示我的图并使用保存按钮手动将其保存为.png,我会在此处获得此图: 大小:1198x803,99 DPI

piechart from manually saving

正如你所看到的那样,第一张图片并没有包含第一张饼图的图例颜色,其他传说真的被替换了。

如何使用plt.savefig('piechart.png')命令获得与第二张图片中相同的数字?

这是我的MCVE代码:

import matplotlib.pyplot as plt


internCounter = 0
kundeCounter = 0
newCounter = 0
closedCounter = 0
developingCounter = 0
testingCounter = 0
tbdCounter = 0
HWCounter = 0
SWCounter = 0
tClosedCounter = 0
tOpenCounter = 0

plt.style.use('ggplot')
fig = plt.figure()
labels = ('new', 'closed', 'developing', 'testing', 'tbd')
sizes = [1, 2, 3, 4, 5]
colors = ['#F5FC34', '#52FC34', '#FAAB23', '#237DFA', '#C863FA', ]
explode = (0, 0, 0, 0, 0) 
ax1 = fig.add_subplot(2,2,1)
ax1.pie(sizes, explode=explode, colors=colors, autopct='%1.1f%%',shadow=True, startangle=90)
ax1.set_title("Status")
ax1.legend(labels, bbox_to_anchor=(0.05, 0.8))

ax2 = fig.add_subplot(2,2,2)            
labels = ('Intern', 'Kunde')
sizes = [1,2]
colors = ['#FAF063', '#63BBFA']
explode = (0, 0) 
ax2.pie(sizes, explode=explode, colors=colors, autopct='%1.1f%%',shadow=True, startangle=90)
ax2.set_title("Department")
ax2.legend(labels, bbox_to_anchor=(1.25, 0.65))

ax3 = fig.add_subplot(2,2,3)            
labels = ('HW', 'SW')
sizes = [1,2]
colors = ['#6A0D7D', '#503C3C']
explode = (0, 0) 
ax3.pie(sizes, explode=explode, colors=colors, autopct='%1.1f%%',shadow=True, startangle=90)
ax3.set_title("Origin")
ax3.legend(labels, bbox_to_anchor=(0, 0.6))

ax4 = fig.add_subplot(2,2,4)            
labels = ('Closed', 'Open')
sizes = [1,2]
colors = ['#58FB4C', '#F8183A']
explode = (0, 0) 
ax4.pie(sizes, explode=explode, colors=colors, autopct='%1.1f%%',shadow=True, startangle=90)
ax4.set_title("Closed-Status")
ax4.legend(labels, bbox_to_anchor=(1.25, 0.65))

plt.savefig('piechart.png')

1 个答案:

答案 0 :(得分:1)

以下内容在“show”和“savefig”中给出了非常相似的外观。我重新安排了你的代码只是为了帮助我查看它,但我相信我的两个不同之处可能对你有所帮助:定义数字大小以确保它有传说的空间,并在bbox_inches="tight"中使用savefig {1}}致电。

import matplotlib.pyplot as plt 
plt.style.use('ggplot')

fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2, figsize=(11,10))

labels1 = ('new', 'closed', 'developing', 'testing', 'tbd')
labels2 = ('Intern', 'Kunde')
labels3 = ('HW', 'SW')
labels4 = ('Closed', 'Open')

sizes1 = [1, 2, 3, 4, 5]
sizes2 = [1,2]
sizes3 = [1,2]
sizes4 = [1,2]

colors1 = ['#F5FC34', '#52FC34', '#FAAB23', '#237DFA', '#C863FA', ]
colors2 = ['#FAF063', '#63BBFA']
colors3 = ['#6A0D7D', '#503C3C']
colors4 = ['#58FB4C', '#F8183A']

explode1 = (0, 0, 0, 0, 0) 
explode2 = (0, 0) 
explode3 = (0, 0) 
explode4 = (0, 0) 

ax1.pie(sizes1, explode=explode1, colors=colors1, autopct='%1.1f%%',shadow=True, startangle=90)
ax2.pie(sizes2, explode=explode2, colors=colors2, autopct='%1.1f%%',shadow=True, startangle=90)
ax3.pie(sizes3, explode=explode3, colors=colors3, autopct='%1.1f%%',shadow=True, startangle=90)
ax4.pie(sizes4, explode=explode4, colors=colors4, autopct='%1.1f%%',shadow=True, startangle=90)

ax1.set_title("Status")
ax2.set_title("Department")
ax3.set_title("Origin")
ax4.set_title("Closed-Status")

ax1.legend(labels1, bbox_to_anchor=(0.05, 0.8))
ax2.legend(labels2, bbox_to_anchor=(1.25, 0.65))
ax3.legend(labels3, bbox_to_anchor=(0, 0.6))
ax4.legend(labels4, bbox_to_anchor=(1.25, 0.65))

plt.show()

fig.savefig('/Users/ite1/Desktop/test.png', bbox_inches="tight")