未修改前的代碼:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
plt.style.use('dark_background')
for i in range(2):
if i ==0:
labels = 'A', 'B', 'C'
fracs = [20, 30, 50]
plt.pie(x=fracs, labels=labels, autopct='%3.1f %%',
shadow=True, labeldistance=1.1, startangle=90, pctdistance=0.6 )
plt.savefig("temp1.png")
if i ==1:
labels = 'Q', 'W', 'E'
fracs = [30, 30, 40]
plt.pie(x=fracs, labels=labels, autopct='%3.1f %%',
shadow=True, labeldistance=1.1, startangle=90, pctdistance=0.6 )
plt.savefig("temp2.png")
然後打開兩個文件查看結果 :
第一個圖正常,接下來查看第二個圖:
很明顯,在畫新一個圖的時候,和上一個圖混在一起了,所以代碼作了以下修改:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
plt.style.use('dark_background')
for i in range(2):
if i ==0:
labels = 'A', 'B', 'C'
fracs = [20, 30, 50]
plt.pie(x=fracs, labels=labels, autopct='%3.1f %%',
shadow=True, labeldistance=1.1, startangle=90, pctdistance=0.6 )
plt.savefig("temp1.png")
plt.clf()#添加上這一行,畫完第一個圖後,重置一下
if i ==1:
labels = 'Q', 'W', 'E'
fracs = [30, 30, 40]
plt.pie(x=fracs, labels=labels, autopct='%3.1f %%',
shadow=True, labeldistance=1.1, startangle=90, pctdistance=0.6 )
plt.savefig("temp2.png")
查看結果:
搞定。