繁体   English   中英

matplotlib 在控制台/运行中工作,但在 Pycharm 的调试中抛出 TypeError

[英]matplotlib works in console/run but throws TypeError in debug in Pycharm

我正在尝试使用 PyCharm 2022.1.1 社区版、Python 3.10、matplotlib 3.5.2 中的 matplotlib 生成一个简单的财务数据图,代码如下:

import matplotlib.pyplot as plt
import yfinance as yf
ticker = 'F'
yfObj = yf.Ticker(ticker)
data = yfObj.history(start='2010-01-01', end='2010-07-01')
plt.figure(figsize=(15, 8))
plt.plot(data['Close'])
plt.show()

代码直接在控制台中按预期执行,并在 pycharm IDE 中“运行”时执行。

但是,当在与运行模式相同的 venv 中以调试模式执行时,相同的代码会抛出TypeError: 'NoneType' object is not callable after has shown an empty matplotlib window. 生成错误的是最终命令 plt.show() - 在调试窗口中顺序执行时,前几行没有问题。

作为比较,下面的基本 matplotlib 图表在控制台、运行和调试窗口中运行良好:

import matplotlib.pyplot as plt
plt.ion()
plt.plot([1.6, 2.7])

必须有一个 venv 设置或行为会影响这一点,并且由我试图在调试模式下显示的特定数据触发 - 但尝试了各种配置后我无法识别它。

谁能建议正在发生的事情和可能的解决方案?

下面的完整回溯错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\david\Python VENVs\lib\site-packages\numpy\core\getlimits.py", line 459, in __new__
    dtype = numeric.dtype(dtype)
TypeError: 'NoneType' object is not callable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\david\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "C:\Users\david\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 839, in callit
    func(*args)
  File "C:\Users\david\Python VENVs\lib\site-packages\matplotlib\backends\_backend_tk.py", line 252, in idle_draw
    self.draw()
  File "C:\Users\david\Python VENVs\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
    super().draw()
  File "C:\Users\david\Python VENVs\lib\site-packages\matplotlib\backends\backend_agg.py", line 436, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\david\Python VENVs\lib\site-packages\matplotlib\artist.py", line 73, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "C:\Users\david\Python VENVs\lib\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "C:\Users\david\Python VENVs\lib\site-packages\matplotlib\figure.py", line 2837, in draw
    mimage._draw_list_compositing_images(
  File "C:\Users\david\Python VENVs\lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\david\Python VENVs\lib\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "C:\Users\david\Python VENVs\lib\site-packages\matplotlib\axes\_base.py", line 3029, in draw
    self._unstale_viewLim()
  File "C:\Users\david\Python VENVs\lib\site-packages\matplotlib\axes\_base.py", line 777, in _unstale_viewLim
    self.autoscale_view(**{f"scale{name}": scale
  File "C:\Users\david\Python VENVs\lib\site-packages\matplotlib\axes\_base.py", line 2937, in autoscale_view
    handle_single_axis(
  File "C:\Users\david\Python VENVs\lib\site-packages\matplotlib\axes\_base.py", line 2933, in handle_single_axis
    x0, x1 = locator.view_limits(x0, x1)
  File "C:\Users\david\Python VENVs\lib\site-packages\matplotlib\ticker.py", line 1663, in view_limits
    return mtransforms.nonsingular(vmin, vmax)
  File "C:\Users\david\Python VENVs\lib\site-packages\matplotlib\transforms.py", line 2880, in nonsingular
    if maxabsvalue < (1e6 / tiny) * np.finfo(float).tiny:
  File "C:\Users\david\Python VENVs\lib\site-packages\numpy\core\getlimits.py", line 462, in __new__
    dtype = numeric.dtype(type(dtype))
TypeError: 'NoneType' object is not callable

所以事实证明这是一个已知的 PyCharm 回归,它会影响 Python 3.10(不是 3.9 或更早版本)。

PyCharm 票在这里: https ://youtrack.jetbrains.com/issue/PY-52654/TypeError-NoneType-object-is-not-callable-when-building-plots-with-the-debugger-using-Python- 310

所以现阶段唯一的解决办法就是降级到 Python 3.9

感谢您指出这一点! 我也遇到了同样的问题。 Pycharm 调试器不适用于 plt.show() ,而它适用于简单的运行。 从 jupyter notebook 复制代码后,这发生在我身上。

如您所说,截至 2022 年 7 月的解决方案是降级到 Python 3.9!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM