matplotlib:属性错误:'Figure'对象没有属性'write'


import matplotlib.pyplot as plt
import numpy as np
from compare_plots import compare_plots
def make_plot(filename,totals):
dict = {2 : 0, 3 : 0, 4 : 0, 5 : 0, 6 : 0, 7 : 0, 8 : 0, 9 : 0, 10 : 0, 11 : 0, 12 : 0}
for i in totals :
dict[i] += 1
filename = plt.figure()
ax = plt.axes()
ax.hist(dict.values(), dict.keys())
ax.set_xlabel('Total')
ax.set_ylabel('Counts')
plt.tight_layout()
plt.savefig(filename)
if __name__=='__main__':
r1,r2=np.loadtxt('rolls.dat',unpack=True,delimiter=',')
totals=r1+r2
make_plot('my_plot.png',totals)
if compare_plots('my_plot.png','instructors_plot.png'):
print('Success!')
else:
print('Keep trying.')

这段代码将数据从读取到两个numpy数组中,然后将这两个数组相加,并将其放入一个函数中,该函数应该将总数绘制为直方图。然后,它调用一个函数来将绘图与它应该看起来的样子进行比较。

运行时,程序会产生错误:AttributeError: 'Figure' object has no attribute 'write'我以前已经设法修复了这个错误,但我似乎记不起我是如何做到的。

非常感谢您的帮助。

通过将第15行plt.savefig(filename)中的filename更改为f字符串f'{filename}'以使该行变为plt.savefig(f'{filename}')来修复错误

最新更新