Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Better Matplotlib support in Jupyter

    General Code/Help
    2
    3
    1983
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      Søren Pallesen last edited by

      I want to turn off inline plots in Jupyter because it ruins/distorts the quality of the chart. Normally in Jupyter you just issue a %matplotlib in the beginning of your script to get the plot to pop in a separate window in much better quality.

      However doing this in Backtrader results in:

      C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\backtrader\plot\__init__.py:30: UserWarning: 
      This call to matplotlib.use() has no effect because the backend has already
      been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
      or matplotlib.backends is imported for the first time.
      
      The backend was *originally* set to 'Qt5Agg' by the following code:
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\runpy.py", line 193, in _run_module_as_main
          "__main__", mod_spec)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\runpy.py", line 85, in _run_code
          exec(code, run_globals)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
          app.launch_new_instance()
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
          app.start()
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\ipykernel\kernelapp.py", line 478, in start
          self.io_loop.start()
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
          super(ZMQIOLoop, self).start()
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\tornado\ioloop.py", line 888, in start
          handler_func(fd_obj, events)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
          return fn(*args, **kwargs)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
          self._handle_recv()
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
          self._run_callback(callback, msg)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
          callback(*args, **kwargs)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
          return fn(*args, **kwargs)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\ipykernel\kernelbase.py", line 281, in dispatcher
          return self.dispatch_shell(stream, msg)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\ipykernel\kernelbase.py", line 232, in dispatch_shell
          handler(stream, idents, msg)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\ipykernel\kernelbase.py", line 397, in execute_request
          user_expressions, allow_stdin)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\ipykernel\ipkernel.py", line 208, in do_execute
          res = shell.run_cell(code, store_history=store_history, silent=silent)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\ipykernel\zmqshell.py", line 533, in run_cell
          return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\interactiveshell.py", line 2728, in run_cell
          interactivity=interactivity, compiler=compiler, result=result)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\interactiveshell.py", line 2850, in run_ast_nodes
          if self.run_code(code, result):
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\interactiveshell.py", line 2910, in run_code
          exec(code_obj, self.user_global_ns, self.user_ns)
        File "<ipython-input-1-d92e3213a0f2>", line 2, in <module>
          get_ipython().run_line_magic('matplotlib', '')
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\interactiveshell.py", line 2095, in run_line_magic
          result = fn(*args,**kwargs)
        File "<decorator-gen-108>", line 2, in matplotlib
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\magic.py", line 187, in <lambda>
          call = lambda f, *a, **k: f(*a, **k)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\magics\pylab.py", line 99, in matplotlib
          gui, backend = self.shell.enable_matplotlib(args.gui)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\interactiveshell.py", line 2978, in enable_matplotlib
          pt.activate_matplotlib(backend)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\pylabtools.py", line 308, in activate_matplotlib
          matplotlib.pyplot.switch_backend(backend)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\matplotlib\pyplot.py", line 232, in switch_backend
          matplotlib.use(newbackend, warn=False, force=True)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\matplotlib\__init__.py", line 1305, in use
          reload(sys.modules['matplotlib.backends'])
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\importlib\__init__.py", line 166, in reload
          _bootstrap._exec(spec, module)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\matplotlib\backends\__init__.py", line 14, in <module>
          line for line in traceback.format_stack()
      
      
        matplotlib.use('TkAgg')
      C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\backtrader\plot\plot.py:127: UserWarning: 
      This call to matplotlib.use() has no effect because the backend has already
      been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
      or matplotlib.backends is imported for the first time.
      
      The backend was *originally* set to 'Qt5Agg' by the following code:
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\runpy.py", line 193, in _run_module_as_main
          "__main__", mod_spec)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\runpy.py", line 85, in _run_code
          exec(code, run_globals)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
          app.launch_new_instance()
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
          app.start()
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\ipykernel\kernelapp.py", line 478, in start
          self.io_loop.start()
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
          super(ZMQIOLoop, self).start()
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\tornado\ioloop.py", line 888, in start
          handler_func(fd_obj, events)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
          return fn(*args, **kwargs)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
          self._handle_recv()
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
          self._run_callback(callback, msg)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
          callback(*args, **kwargs)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
          return fn(*args, **kwargs)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\ipykernel\kernelbase.py", line 281, in dispatcher
          return self.dispatch_shell(stream, msg)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\ipykernel\kernelbase.py", line 232, in dispatch_shell
          handler(stream, idents, msg)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\ipykernel\kernelbase.py", line 397, in execute_request
          user_expressions, allow_stdin)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\ipykernel\ipkernel.py", line 208, in do_execute
          res = shell.run_cell(code, store_history=store_history, silent=silent)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\ipykernel\zmqshell.py", line 533, in run_cell
          return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\interactiveshell.py", line 2728, in run_cell
          interactivity=interactivity, compiler=compiler, result=result)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\interactiveshell.py", line 2850, in run_ast_nodes
          if self.run_code(code, result):
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\interactiveshell.py", line 2910, in run_code
          exec(code_obj, self.user_global_ns, self.user_ns)
        File "<ipython-input-1-d92e3213a0f2>", line 2, in <module>
          get_ipython().run_line_magic('matplotlib', '')
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\interactiveshell.py", line 2095, in run_line_magic
          result = fn(*args,**kwargs)
        File "<decorator-gen-108>", line 2, in matplotlib
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\magic.py", line 187, in <lambda>
          call = lambda f, *a, **k: f(*a, **k)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\magics\pylab.py", line 99, in matplotlib
          gui, backend = self.shell.enable_matplotlib(args.gui)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\interactiveshell.py", line 2978, in enable_matplotlib
          pt.activate_matplotlib(backend)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\IPython\core\pylabtools.py", line 308, in activate_matplotlib
          matplotlib.pyplot.switch_backend(backend)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\matplotlib\pyplot.py", line 232, in switch_backend
          matplotlib.use(newbackend, warn=False, force=True)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\matplotlib\__init__.py", line 1305, in use
          reload(sys.modules['matplotlib.backends'])
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\importlib\__init__.py", line 166, in reload
          _bootstrap._exec(spec, module)
        File "C:\Users\Soren\Anaconda3\envs\CCXTbt2\lib\site-packages\matplotlib\backends\__init__.py", line 14, in <module>
          line for line in traceback.format_stack()
      
      
        matplotlib.use('nbagg')
      

      I should say that it does actually pop the chart in a new window but I would like to get rid of the errors in the trace. Any ideas?

      What is Matplotlib instantiated as in Backtrader?

      1 Reply Last reply Reply Quote 0
      • P
        Paska Houso last edited by

        • https://www.backtrader.com/docu/plotting/plotting.html

        iplot=False

        1 Reply Last reply Reply Quote 1
        • S
          Søren Pallesen last edited by

          @Paska-Houso said in Better Matplotlib support in Jupyter:

          iplot=False

          Sorry missed that one in the docs. Damm love this community !!! Thx!

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors