'matplotlib: AttributeError: 'Figure' object has no attribute '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.')
This code reads data from into two numpy arrays which are then totaled together and thrown into a function that is supposed to plot the totals as a histogram. It then calls a function to compare the plot with what it's supposed to look like.
When run, the program produces the error: AttributeError: 'Figure' object has no attribute 'write'
I've managed to fix this error previously but I can't seem to recall how I did it.
Any help is greatly appreciated.
Solution 1:[1]
Error is fixed by changing filename
in line 15 plt.savefig(filename)
into an f-string, f'{filename}'
so that the line becomes plt.savefig(f'{filename}')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | BConn63 |