簡體   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