【问题标题】:I get TypeError: can't multiply sequence by non-int of type 'numpy.float64' when trying to save a plot in Python [closed]我得到 TypeError: can't multiply sequence by non-int of type 'numpy.float64' when trying to save a plot in Python [关闭]
【发布时间】:2021-01-06 21:56:51
【问题描述】:
# 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

如果我使用我的 32 位版本的 python,情节可以正常工作,但是当我使用 pickle 时出现内存错误,所以我切换到 64 位 python。但是现在当我尝试保存绘图时出现此错误。

【问题讨论】:

    标签: python-3.x numpy matplotlib 32bit-64bit


    【解决方案1】:

    阅读savefig (https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html) 的文档。

    dpi 参数指定为 float,而您传递的是 string

    尝试使用 dpi=300(不带撇号)。

    【讨论】:

    • 谢谢您的回答。那个错误就是我,我总是找借口不再寻找这样简单的东西......
    猜你喜欢
    • 1970-01-01
    • 2013-08-25
    • 2018-01-27
    • 2016-01-27
    • 2011-04-02
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    相关资源
    最近更新 更多