matplotlib 在 console/run 中工作,但在 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()

代码直接在控制台中按预期执行,当 'Run' 来自 pycharm IDE.

但是,当在与 运行 模式相同的 venv 中以调试模式执行时,相同的代码在显示空的 matplotlib window 后抛出 TypeError: 'NoneType' object is not callable。产生错误的是最后一个命令 plt.show() - 在调试 window.

中按顺序依次执行前几行没有问题

为了比较,下面的基本 matplotlib 图表在控制台中工作正常,运行 和调试 windows:

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

所以现阶段唯一的解决办法就是降级到Python3.9