简体   繁体   中英

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:

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.

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. 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.

For comparison, the basic matplotlib chart below works fine in the console, run and debug windows:

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.

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).

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

So the only solution at this stage is to downgrade to 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. This happened to me after copying code from a jupyter notebook.

Solution as of July 2022 is to downgrade to Python 3.9 as stated by you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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