Ran into another issues today whereby the plots stopped showing up in my Jupyter notebooks (using just cerebro.plot()
. On a hunch, I decided to try cerebro.plot(iplot=False)
inside the Jupyter notebook to see what would happen. As long as I have the majick %matplotlib inline
somewhere in my notebook, that issue is also fixed.

Best posts made by Joe Landers
-
RE: Cerebro plot failed to run
-
RE: Cerebro plot failed to run
@run-out However, if you're asking whether I found a better way to detect whether or not we're running inside a notebook? No, I didn't. However, I think the better answer is to not auto-detect at all. Most Jupyter users are quite familiar with using the
%matplotlib inline
majik when plotting in Jupyter. However, this auto-detection foobar is messing that up too. -
RE: Cerebro plot failed to run
I realize this topic is pretty old, but since I ran into this error myself recently....
It's incorrect to say:
Something is broken well beyond the realm of backtrader
While it's true that this error occurs in the guts of matplotlib, it happens because Backtrader is incorrectly assuming it's running inside a Jupyter notebook and therefore configuring matplotlib with an incorrect backend.
The fix is to use
cerebro.plot(iplot=False)
when not running inside a Jupyter notebook.See:
https://www.backtrader.com/blog/posts/2016-09-17-notebook-inline/notebook-inline/
and:
https://community.backtrader.com/topic/2106/cerebro-plot-error/4
Latest posts made by Joe Landers
-
RE: Cerebro plot failed to run
Ran into another issues today whereby the plots stopped showing up in my Jupyter notebooks (using just
cerebro.plot()
. On a hunch, I decided to trycerebro.plot(iplot=False)
inside the Jupyter notebook to see what would happen. As long as I have the majick%matplotlib inline
somewhere in my notebook, that issue is also fixed. -
RE: Cerebro plot failed to run
@run-out However, if you're asking whether I found a better way to detect whether or not we're running inside a notebook? No, I didn't. However, I think the better answer is to not auto-detect at all. Most Jupyter users are quite familiar with using the
%matplotlib inline
majik when plotting in Jupyter. However, this auto-detection foobar is messing that up too. -
RE: Cerebro plot failed to run
@run-out Yes, I found that using
cerebro.plot(iplot=False)
when not running in a notebook worked. Also, this is only needed (for me at least) if the Jupyter server happens to be running (even in the background) at the time.Interestingly, I had another problem whereby matplotlib plots would crash whenever I scrolled. I Googled around learned it's because I installed Python from Homebrew on my mac, and that version of python doesn't have the same gui toolkit as the "full" version. I uninstalled that version of python and installed the official distribution from python.org and now both problems are gone ¯_(ツ)_/¯
-
RE: Cerebro plot failed to run
PS. Just to clarify, I had been running Backtrader at the command line with no errors for quite some time. Then I was working inside a Jupyter notebook and suddenly my CLI code started throwing the same error mentioned above...
AttributeError: 'NoneType' object has no attribute 'session'
Some googling around led me to know it happens when matplotlib is configured with the wrong backend. Further googling led me to the solution, whereby Backtrader chooses an incorrect matplotlib backend. The offending code is at line 125 of plot.py:
if iplot: if 'ipykernel' in sys.modules: matplotlib.use('nbagg')
Once I had installed Jupyter on my system, and started using it, that's when the error began. So, my guess is we need some other way to detect whether or not we're running inside a notebook.
-
RE: Cerebro plot failed to run
I realize this topic is pretty old, but since I ran into this error myself recently....
It's incorrect to say:
Something is broken well beyond the realm of backtrader
While it's true that this error occurs in the guts of matplotlib, it happens because Backtrader is incorrectly assuming it's running inside a Jupyter notebook and therefore configuring matplotlib with an incorrect backend.
The fix is to use
cerebro.plot(iplot=False)
when not running inside a Jupyter notebook.See:
https://www.backtrader.com/blog/posts/2016-09-17-notebook-inline/notebook-inline/
and:
https://community.backtrader.com/topic/2106/cerebro-plot-error/4