Esempio n. 1
0
def configure_inline_support(shell, backend):
    """Configure an IPython shell object for matplotlib use.

    Parameters
    ----------
    shell : InteractiveShell instance

    backend : matplotlib backend
    """
    # If using our svg payload backend, register the post-execution
    # function that will pick up the results for display.  This can only be
    # done with access to the real shell object.

    # Note: if we can't load the inline backend, then there's no point
    # continuing (such as in terminal-only shells in environments without
    # zeromq available).
    try:
        from IPython.kernel.zmq.pylab.backend_inline import InlineBackend
    except ImportError:
        return
    from matplotlib import pyplot

    cfg = InlineBackend.instance(parent=shell)
    cfg.shell = shell
    if cfg not in shell.configurables:
        shell.configurables.append(cfg)

    if backend == backends['inline']:
        from IPython.kernel.zmq.pylab.backend_inline import flush_figures
        shell.events.register('post_execute', flush_figures)

        # Save rcParams that will be overwrittern
        shell._saved_rcParams = dict()
        for k in cfg.rc:
            shell._saved_rcParams[k] = pyplot.rcParams[k]
        # load inline_rc
        pyplot.rcParams.update(cfg.rc)
    else:
        from IPython.kernel.zmq.pylab.backend_inline import flush_figures
        try:
            shell.events.unregister('post_execute', flush_figures)
        except ValueError:
            pass
        if hasattr(shell, '_saved_rcParams'):
            pyplot.rcParams.update(shell._saved_rcParams)
            del shell._saved_rcParams

    # Setup the default figure format
    select_figure_formats(shell, cfg.figure_formats, **cfg.print_figure_kwargs)
def configure_inline_support(shell, backend):
    """Configure an IPython shell object for matplotlib use.

    Parameters
    ----------
    shell : InteractiveShell instance

    backend : matplotlib backend
    """
    # If using our svg payload backend, register the post-execution
    # function that will pick up the results for display.  This can only be
    # done with access to the real shell object.

    # Note: if we can't load the inline backend, then there's no point
    # continuing (such as in terminal-only shells in environments without
    # zeromq available).
    try:
        from IPython.kernel.zmq.pylab.backend_inline import InlineBackend
    except ImportError:
        return
    from matplotlib import pyplot

    cfg = InlineBackend.instance(parent=shell)
    cfg.shell = shell
    if cfg not in shell.configurables:
        shell.configurables.append(cfg)

    if backend == backends['inline']:
        from IPython.kernel.zmq.pylab.backend_inline import flush_figures
        shell.events.register('post_execute', flush_figures)

        # Save rcParams that will be overwrittern
        shell._saved_rcParams = dict()
        for k in cfg.rc:
            shell._saved_rcParams[k] = pyplot.rcParams[k]
        # load inline_rc
        pyplot.rcParams.update(cfg.rc)
    else:
        from IPython.kernel.zmq.pylab.backend_inline import flush_figures
        try:
            shell.events.unregister('post_execute', flush_figures)
        except ValueError:
            pass
        if hasattr(shell, '_saved_rcParams'):
            pyplot.rcParams.update(shell._saved_rcParams)
            del shell._saved_rcParams

    # Setup the default figure format
    select_figure_formats(shell, cfg.figure_formats, **cfg.print_figure_kwargs)
Esempio n. 3
0
def set_matplotlib_close(close):
    """Set whether the inline backend closes all figures automatically or not.
    
    By default, the inline backend used in the IPython Notebook will close all
    matplotlib figures automatically after each cell is run. This means that
    plots in different cells won't interfere. Sometimes, you may want to make
    a plot in one cell and then refine it in later cells. This can be accomplished
    by::
    
        In [1]: set_matplotlib_close(False)
    
    To set this in your config files use the following::
    
        c.InlineBackend.close_figures = False
    
    Parameters
    ----------
    close : bool
        Should all matplotlib figures be automatically closed after each cell is
        run?
    """
    from IPython.kernel.zmq.pylab.backend_inline import InlineBackend
    ilbe = InlineBackend.instance()
    ilbe.close_figures = close
Esempio n. 4
0
def set_matplotlib_close(close):
    """Set whether the inline backend closes all figures automatically or not.
    
    By default, the inline backend used in the IPython Notebook will close all
    matplotlib figures automatically after each cell is run. This means that
    plots in different cells won't interfere. Sometimes, you may want to make
    a plot in one cell and then refine it in later cells. This can be accomplished
    by::
    
        In [1]: set_matplotlib_close(False)
    
    To set this in your config files use the following::
    
        c.InlineBackend.close_figures = False
    
    Parameters
    ----------
    close : bool
        Should all matplotlib figures be automatically closed after each cell is
        run?
    """
    from IPython.kernel.zmq.pylab.backend_inline import InlineBackend
    ilbe = InlineBackend.instance()
    ilbe.close_figures = close
Esempio n. 5
0
def configure_inline_support(shell, backend, user_ns=None):
    """Configure an IPython shell object for matplotlib use.

    Parameters
    ----------
    shell : InteractiveShell instance

    backend : matplotlib backend

    user_ns : dict
      A namespace where all configured variables will be placed.  If not given,
      the `user_ns` attribute of the shell object is used.
    """
    # If using our svg payload backend, register the post-execution
    # function that will pick up the results for display.  This can only be
    # done with access to the real shell object.

    # Note: if we can't load the inline backend, then there's no point
    # continuing (such as in terminal-only shells in environments without
    # zeromq available).
    try:
        from IPython.kernel.zmq.pylab.backend_inline import InlineBackend
    except ImportError:
        return
    from matplotlib import pyplot

    user_ns = shell.user_ns if user_ns is None else user_ns
    
    cfg = InlineBackend.instance(config=shell.config)
    cfg.shell = shell
    if cfg not in shell.configurables:
        shell.configurables.append(cfg)

    if backend == backends['inline']:
        from IPython.kernel.zmq.pylab.backend_inline import flush_figures
        shell.register_post_execute(flush_figures)

        # Save rcParams that will be overwrittern
        shell._saved_rcParams = dict()
        for k in cfg.rc:
            shell._saved_rcParams[k] = pyplot.rcParams[k]
        # load inline_rc
        pyplot.rcParams.update(cfg.rc)
        # Add 'figsize' to pyplot and to the user's namespace
        user_ns['figsize'] = pyplot.figsize = figsize
    else:
        from IPython.kernel.zmq.pylab.backend_inline import flush_figures
        if flush_figures in shell._post_execute:
            shell._post_execute.pop(flush_figures)
        if hasattr(shell, '_saved_rcParams'):
            pyplot.rcParams.update(shell._saved_rcParams)
            del shell._saved_rcParams

    # Setup the default figure format
    fmt = cfg.figure_format
    select_figure_format(shell, fmt)

    # The old pastefig function has been replaced by display
    from IPython.core.display import display
    # Add display and getfigs to the user's namespace
    user_ns['display'] = display
    user_ns['getfigs'] = getfigs
    def base16_mplrc(self,args):
        #parse the magick arguments
        args = parse_argstring(self.base16_mplrc,args)
        shade = args.shade
        theme = args.theme

        #detect the base16 ipython notebook theme, setup the matplotlib rc
        css_theme = None
        css_shade = None
        custom_css_fname = self.shell.profile_dir.location+'/static/custom/custom.css'
        if os.path.exists(custom_css_fname):
            with open(custom_css_fname) as css_file:
                for line in css_file:
                    if(re.match('^\s*Name: ',line)):
                        css_theme = line.split()[2].lower()
                        css_shade = line.split()[-1].lower()

        #fall back on sensible defaults
        if theme is None:
            theme = css_theme
        if shade is None:
            shade = css_shade
        if theme is None:
            print('''
                     Could not detect base-16 ipython notebook theme. Download base16 theme notebook theme
                     from https://github.com/nsonnad/base16-ipython-notebook . Using \'default\' theme.''')
            theme='default'

        if shade is None:
            print('''
                     Could not detect base-16 ipython notebook theme shade. Download base16 theme notebook themes
                     from https://github.com/nsonnad/base16-ipython-notebook . Using \'default\' theme.''')
            shade = 'light'

        avail_themes = [os.path.split(f)[-1].split('.')[0] for f in glob.glob(self.shell.ipython_dir+'/extensions/base16-mplrc-themes/*.json')]
        #validate input
        if shade not in ['dark','light']:
            print("shade must be either dark or light, defaulting to light")
            shade = 'light'
        if theme not in avail_themes:
            print("theme must be present in base16-mplrc-themes dir, defaulting to 'default'")
            print("Available themes:")
            for t in avail_themes:
                print("\t{}".format(t))
            theme = 'default'

        print("Setting plotting theme to {}-{}. Palette available in b16_colors".format(theme,shade))

        theme_colors = json.load(open(self.shell.ipython_dir+'/extensions/base16-mplrc-themes/'+theme+'.json'))

        #snag the matplotlibrc configuration from the ipython config
        from IPython.kernel.zmq.pylab.backend_inline import InlineBackend
        cfg = InlineBackend.instance(parent=self.shell)
        cfg.shell=self.shell
        if cfg not in self.shell.configurables:
            self.shell.configurables.append(cfg)
        if shade=="dark":
             cfg.rc = {'figure.facecolor':theme_colors['base00'],
                        'savefig.facecolor':theme_colors['base00'],
                        'text.color':theme_colors['base07'],
                        'axes.color_cycle':[theme_colors['base{:02X}'.format(i)] for i in [13,8,11,9,12,14,10,15]],
                        'axes.facecolor': theme_colors['base01'],
                        'axes.edgecolor': theme_colors['base01'],
                        'axes.labelcolor': theme_colors['base07'],
                        'lines.color': theme_colors['base09'],
                        'lines.markeredgewidth': 0,
                        'patch.facecolor': theme_colors['base09'],
                        'patch.edgecolor': theme_colors['base02'],
                        'xtick.color': theme_colors['base07'],
                        'ytick.color': theme_colors['base07'],
                        'grid.color': theme_colors['base02']}
        elif shade=="light":
            cfg.rc = {'figure.facecolor':theme_colors['base07'],
                      'savefig.facecolor':theme_colors['base07'],
                      'text.color':theme_colors['base00'],
                      'axes.color_cycle':[theme_colors['base{:02X}'.format(i)] for i in [13,8,11,9,12,14,10,15]],
                      'axes.facecolor': theme_colors['base07'],
                      'axes.edgecolor': theme_colors['base00'],
                      'axes.labelcolor': theme_colors['base00'],
                      'lines.color': theme_colors['base0D'],
                      'lines.markeredgewidth': 0,
                      'patch.facecolor': theme_colors['base0D'],
                      'patch.edgecolor': theme_colors['base02'],
                      'xtick.color': theme_colors['base00'],
                      'ytick.color': theme_colors['base00'],
                      'grid.color': theme_colors['base06']}
        #If pyplot is already using the InlineBackend, this will force an update to the rcParams

        from matplotlib import pyplot, cm
        from matplotlib.colors import ColorConverter, ListedColormap
        import numpy as np

        conv = ColorConverter()
        if pyplot.rcParams['backend'] == 'module://IPython.kernel.zmq.pylab.backend_inline':

            #push the color pallete into scope for the user
            full=['red','orange','yellow','green','cyan','blue','magenta','brown']
            abbr=['r','o','y','g','c','b','m','n']
            #create a color palette class
            class Palette(object): pass
            b16_colors=Palette()
            for f,a,i in zip(full,abbr,range(8,16)):
                setattr(b16_colors,f,conv.to_rgb(theme_colors['base{:02X}'.format(i)]))
                setattr(b16_colors,a,conv.to_rgb(theme_colors['base{:02X}'.format(i)]))

            setattr(b16_colors,'white',conv.to_rgb(theme_colors['base07']))
            setattr(b16_colors,'w',conv.to_rgb(theme_colors['base07']))
            setattr(b16_colors,'black',conv.to_rgb(theme_colors['base00']))
            setattr(b16_colors,'k',conv.to_rgb(theme_colors['base00']))

            #----------------- Color maps ---------------------#
            def make_gradient(cols):
                N=255
                M=int(np.ceil(N/len(cols)))
                reds = np.empty((0),dtype=np.float)
                blues = np.empty((0),dtype=np.float)
                greens = np.empty((0),dtype=np.float)
                for c0,c1 in zip(cols[:-1],cols[1:]):
                    reds = np.concatenate((reds,np.linspace(c0[0],c1[0],M-1)))
                    greens = np.concatenate((greens,np.linspace(c0[1],c1[1],M-1)))
                    blues = np.concatenate((blues,np.linspace(c0[2],c1[2],M-1)))
                return np.array((reds,greens,blues)).transpose()

            #Make a "jet" colormap
            cols =[b16_colors.b,
                   b16_colors.c,
                   b16_colors.g,
                   b16_colors.y,
                   b16_colors.o,
                   b16_colors.r]
            b16_colors.jet = ListedColormap(make_gradient(cols),name='b16_jet')
            cm.register_cmap('b16_jet',b16_colors.jet)

            #Make a "grayscale" colormap
            cols = [conv.to_rgb(theme_colors['base{:02X}'.format(i)]) for i in range(8)]
            b16_colors.gray = ListedColormap(make_gradient(cols),name='b16_gray')
            cm.register_cmap('b16_gray',b16_colors.gray)

            #Make a "blues" colormap
            cols = [b16_colors.w,b16_colors.c,b16_colors.b]
            b16_colors.blues = ListedColormap(make_gradient(cols),name='b16_blues')
            cm.register_cmap('b16_blues',b16_colors.blues)

            #Make a "greens" colormap
            cols = [b16_colors.w,b16_colors.c,b16_colors.g]
            b16_colors.greens = ListedColormap(make_gradient(cols),name='b16_greens')
            cm.register_cmap('b16_greens',b16_colors.greens)

            #Make a "oranges" colormap
            cols = [b16_colors.w,b16_colors.y,b16_colors.o]
            b16_colors.oranges = ListedColormap(make_gradient(cols),name='b16_oranges')
            cm.register_cmap('b16_oranges',b16_colors.oranges)

            #Make a "reds" colormap
            cols = [b16_colors.w,b16_colors.y,b16_colors.o,b16_colors.r]
            b16_colors.reds = ListedColormap(make_gradient(cols),name='b16_reds')
            cm.register_cmap('b16_reds',b16_colors.reds)

            #Make a "flame" colormap
            cols = [conv.to_rgb(theme_colors['base{:02X}'.format(i)]) for i in range(0,3,2)]+\
                   [b16_colors.y,b16_colors.o,b16_colors.r]
            b16_colors.flame = ListedColormap(make_gradient(cols),name='b16_flame')
            cm.register_cmap('b16_flame',b16_colors.flame)

            #Make a "brbg" colormap
            cols = [b16_colors.n,b16_colors.w,b16_colors.b,b16_colors.g]
            b16_colors.brbg = ListedColormap(make_gradient(cols),name='b16_brbg')
            cm.register_cmap('b16_brbg',b16_colors.brbg)

            self.shell.push({"b16_colors":b16_colors})
            cfg.rc.update({'image.cmap':'b16_flame'})

            pyplot.rcParams.update(cfg.rc)