def set_matplotlib_formats(*formats, **kwargs): """Select figure formats for the inline backend. Optionally pass quality for JPEG. For example, this enables PNG and JPEG output with a JPEG quality of 90%:: In [1]: set_matplotlib_formats('png', 'jpeg', quality=90) To set this in your config files use the following:: c.InlineBackend.figure_formats = {'png', 'jpeg'} c.InlineBackend.print_figure_kwargs.update({'quality' : 90}) Parameters ---------- *formats : strs One or more figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'. **kwargs : Keyword args will be relayed to ``figure.canvas.print_figure``. """ from IPython.core.interactiveshell import InteractiveShell from IPython.core.pylabtools import select_figure_formats # build kwargs, starting with InlineBackend config kw = {} from ipykernel.pylab.config import InlineBackend cfg = InlineBackend.instance() kw.update(cfg.print_figure_kwargs) kw.update(**kwargs) shell = InteractiveShell.instance() select_figure_formats(shell, formats, **kw)
def set_matplotlib_close(close=True): """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 ipykernel.pylab.config import InlineBackend cfg = InlineBackend.instance() cfg.close_figures = close
def _get_inline_config(): from ipykernel.pylab.config import InlineBackend return InlineBackend.instance()
def base16_mplrc(self,args): """base16_mplrc defines magic invoked with %base16_mplrc. args: theme : base16 theme to use """ # Parse the magic arguments # https://ipython.readthedocs.io/en/stable/api/generated/IPython.core.magic_arguments.html args = parse_argstring(self.base16_mplrc, args) theme = args.theme # Detect the base16 ipython notebook theme, # setup the matplotlib rc css_theme = None custom_css_fname = self.shell.profile_dir.location+'/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() # Fall back on sensible defaults if theme is None: theme = css_theme 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' # TODO: fixme #### old: ###jsondir = '/Users/charles/codes/base16/base16-jupyter/mpljson' # new: jsondir = 'mpljson' avail_themes = [os.path.split(f)[-1].split('.')[0] for f in glob.glob(jsondir + '/*.json')] #validate input 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)) theme_colors = json.load(open(jsondir+'/'+theme+'.json')) #snag the matplotlibrc configuration from the ipython config #### old IPython: ###from IPython.kernel.zmq.pylab.backend_inline import InlineBackend # new ipykernel: from ipykernel.pylab.config 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 True: 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']} #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://ipykernel.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)