简体   繁体   English

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

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

I am trying to generate a simple plot of financial data using matplotlib in PyCharm 2022.1.1 Community Edition, Python 3.10, matplotlib 3.5.2, with the code below:我正在尝试使用 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()

The code executes as expected directly in the console, and when 'Run' from the pycharm IDE.代码直接在控制台中按预期执行,并在 pycharm IDE 中“运行”时执行。

However, when executed in Debug mode in the same venv as the Run mode, the same code throws TypeError: 'NoneType' object is not callable after having displayed an empty matplotlib window.但是,当在与运行模式相同的 venv 中以调试模式执行时,相同的代码会抛出TypeError: 'NoneType' object is not callable after has shown an empty matplotlib window. It is the final command plt.show() that generates the error - the previous lines have no issues when executed one by one sequentially in the debug window.生成错误的是最终命令 plt.show() - 在调试窗口中顺序执行时,前几行没有问题。

For comparison, the basic matplotlib chart below works fine in the console, run and debug windows:作为比较,下面的基本 matplotlib 图表在控制台、运行和调试窗口中运行良好:

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

There must be a venv setting or behaviour that is affecting this, and being triggered by the specific data I'm trying to display in debug mode - but having tried various configurations I haven't been able to identify it.必须有一个 venv 设置或行为会影响这一点,并且由我试图在调试模式下显示的特定数据触发 - 但尝试了各种配置后我无法识别它。

Can anyone suggest what is going on and a possible solution?谁能建议正在发生的事情和可能的解决方案?

Full traceback error below:下面的完整回溯错误:

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

So it turns out this is a known regression with PyCharm that affects Python 3.10 (not 3.9 or earlier).所以事实证明这是一个已知的 PyCharm 回归,它会影响 Python 3.10(不是 3.9 或更早版本)。

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

So the only solution at this stage is to downgrade to Python 3.9所以现阶段唯一的解决办法就是降级到 Python 3.9

Thank you for pointing this out!感谢您指出这一点! I also experienced the same issue.我也遇到了同样的问题。 Pycharm Debugger does not work for plt.show() while it works with a simple run. Pycharm 调试器不适用于 plt.show() ,而它适用于简单的运行。 This happened to me after copying code from a jupyter notebook.从 jupyter notebook 复制代码后,这发生在我身上。

Solution as of July 2022 is to downgrade to Python 3.9 as stated by you!如您所说,截至 2022 年 7 月的解决方案是降级到 Python 3.9!

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

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