Automatically run %matplotlib inline in IPython Notebook

后端 未结 7 1546
逝去的感伤
逝去的感伤 2020-11-28 20:06

Every time I launch IPython Notebook, the first command I run is

%matplotlib inline

Is there some way to change my config file so that when

7条回答
  •  情深已故
    2020-11-28 21:04

    The setting was disabled in Jupyter 5.X and higher by adding below code

    pylab = Unicode('disabled', config=True,
        help=_("""
        DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.
        """)
    )
    
    @observe('pylab')
    def _update_pylab(self, change):
        """when --pylab is specified, display a warning and exit"""
        if change['new'] != 'warn':
            backend = ' %s' % change['new']
        else:
            backend = ''
        self.log.error(_("Support for specifying --pylab on the command line has been removed."))
        self.log.error(
            _("Please use `%pylab{0}` or `%matplotlib{0}` in the notebook itself.").format(backend)
        )
        self.exit(1)
    

    And in previous versions it has majorly been a warning. But this not a big issue because Jupyter uses concepts of kernels and you can find kernel for your project by running below command

    $ jupyter kernelspec list
    Available kernels:
      python3    /Users/tarunlalwani/Documents/Projects/SO/notebookinline/bin/../share/jupyter/kernels/python3
    

    This gives me the path to the kernel folder. Now if I open the /Users/tarunlalwani/Documents/Projects/SO/notebookinline/bin/../share/jupyter/kernels/python3/kernel.json file, I see something like below

    {
     "argv": [
      "python",
      "-m",
      "ipykernel_launcher",
      "-f",
      "{connection_file}",
     ],
     "display_name": "Python 3",
     "language": "python"
    }
    

    So you can see what command is executed to launch the kernel. So if you run the below command

    $ python -m ipykernel_launcher --help
    IPython: an enhanced interactive Python shell.
    
    Subcommands
    -----------
    
    Subcommands are launched as `ipython-kernel cmd [args]`. For information on
    using subcommand 'cmd', do: `ipython-kernel cmd -h`.
    
    install
        Install the IPython kernel
    
    Options
    -------
    
    Arguments that take values are actually convenience aliases to full
    Configurables, whose aliases are listed on the help line. For more information
    on full configurables, see '--help-all'.
    
    ....
    --pylab= (InteractiveShellApp.pylab)
        Default: None
        Choices: ['auto', 'agg', 'gtk', 'gtk3', 'inline', 'ipympl', 'nbagg', 'notebook', 'osx', 'pdf', 'ps', 'qt', 'qt4', 'qt5', 'svg', 'tk', 'widget', 'wx']
        Pre-load matplotlib and numpy for interactive use, selecting a particular
        matplotlib backend and loop integration.
    --matplotlib= (InteractiveShellApp.matplotlib)
        Default: None
        Choices: ['auto', 'agg', 'gtk', 'gtk3', 'inline', 'ipympl', 'nbagg', 'notebook', 'osx', 'pdf', 'ps', 'qt', 'qt4', 'qt5', 'svg', 'tk', 'widget', 'wx']
        Configure matplotlib for interactive use with the default matplotlib
        backend.
    ...    
    To see all available configurables, use `--help-all`
    

    So now if we update our kernel.json file to

    {
     "argv": [
      "python",
      "-m",
      "ipykernel_launcher",
      "-f",
      "{connection_file}",
      "--pylab",
      "inline"
     ],
     "display_name": "Python 3",
     "language": "python"
    }
    

    And if I run jupyter notebook the graphs are automatically inline

    Note the below approach also still works, where you create a file on below path

    ~/.ipython/profile_default/ipython_kernel_config.py

    c = get_config()
    c.IPKernelApp.matplotlib = 'inline'
    

    But the disadvantage of this approach is that this is a global impact on every environment using python. You can consider that as an advantage also if you want to have a common behaviour across environments with a single change.

    So choose which approach you would like to use based on your requirement

提交回复
热议问题