未修改前的代码:
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")
查看结果:
搞定。