Search code examples
python-3.xnumpymatplotlib32bit-64bit

I get TypeError: can't multiply sequence by non-int of type 'numpy.float64' when trying to save a plot in Python


# This is my code that creates the plot
moving_avg = np.convolve(list_of_results, np.ones((100,)) / 100, mode="valid")
plt.plot([i for i in range(len(moving_avg))], moving_avg)
plt.ylabel('Remaining Pins')
plt.xlabel('Games played')
plt.grid()
plt.savefig('English_10000.pdf', dpi='300')
Traceback (most recent call last):
  File "C:/Users/lovir/Desktop/Backup/project_solitaer/display.py", line 144, in <module>
    plt.savefig('English_10000.pdf', dpi='300')
  File "C:\Users\lovir\env\lib\site-packages\matplotlib\pyplot.py", line 859, in savefig
    res = fig.savefig(*args, **kwargs)
  File "C:\Users\lovir\env\lib\site-packages\matplotlib\figure.py", line 2311, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "C:\Users\lovir\env\lib\site-packages\matplotlib\backend_bases.py", line 2162, in print_figure
    with cbook._setattr_cm(self, manager=None), \
  File "C:\Users\lovir\AppData\Local\Programs\Python\Python38\lib\contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "C:\Users\lovir\env\lib\site-packages\matplotlib\cbook\__init__.py", line 2079, in _setattr_cm
    setattr(obj, attr, val)
  File "C:\Users\lovir\env\lib\site-packages\matplotlib\figure.py", line 451, in _set_dpi
    self.dpi_scale_trans.clear().scale(dpi)
  File "C:\Users\lovir\env\lib\site-packages\matplotlib\transforms.py", line 1996, in scale
    self._mtx[0, 0] *= sx
TypeError: can't multiply sequence by non-int of type 'numpy.float64'

Process finished with exit code 1

If I use my 32bit version of python the plot works fine, but I get a memory error when I use pickle, so I switched to 64bit python. But now I get this error when I try to save the plot.


Solution

  • Read documentation of savefig (https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html).

    dpi parameter is specified as float, whereas you passed a string.

    Try with dpi=300 (without apostrophes).