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

Matplotlib: Different views from plt.savefig() and manual saving

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

如果我用 plt.show() 显示我的图形并使用保存按钮将其手动保存为 .png,我会在这里得到这个图形: 尺寸:1198x803,99 DPI

如您所见,第一张图片不包含第一个饼图的图例颜色,其他图例确实被替换了。

如何使用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')

下面给出了“show”和“savefig”中非常相似的外观。我重新排列了你的代码只是为了帮助我查看它,但我相信我的代码有两个不同之处可能对你有帮助:定义图形大小以确保它为图例留出空间,并在 savefig呼唤。

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