Skip to content

Instantly share code, notes, and snippets.

@jfeist
Last active December 14, 2019 11:29
Show Gist options
  • Save jfeist/5c3ae316b6e9afcf9e4b to your computer and use it in GitHub Desktop.
Save jfeist/5c3ae316b6e9afcf9e4b to your computer and use it in GitHub Desktop.
Jupyter tips & tricks

Jupyter Notebook extensions

Very useful collection of extensions for Jupyter Notebook (not JupyterLab though): https://github.com/ipython-contrib/jupyter_contrib_nbextensions - installed on soleus, all the extensions can be activated/configured through the "Nbextensions" tab in the notebook tree view (the "file explorer").

Matplotlib

style files

I have written two matplotlib style files, jf.mplstyle (default line colors based on seaborn with small modifications) and jf_cb.mplstyle (default line colors based on recommendations for viewers with colorblindness from Nature Methods 8, 441 (2011)). To use these, you can either (after having run import matplotlib.pyplot as plt or %pylab inline before)

  • call plt.style.use('https://johannesfeist.eu/misc/jf.mplstyle') in your python notebook to use the style file you want directly from my server.
  • or download the file and save it into into your local matplotlib style directory (~/.matplotlib/stylelib on OS X, ~/.config/matplotlib/stylelib on Linux), and then just use plt.style.use('jf') / plt.style.use('jf_cb'). You can also then modify it to your hearts content, of course (although you might want to rename it in that case).

Note that in either case, some settings from the style (such as the figure size) do not get applied if it is called in the same cell in which matplotlib is imported for the first time (that seems to call some setup code that is run after the contents of the cell), so just put it in a separate cell to avoid surprises.

font

For both style files above, I am using the Latin Modern Math font, which is the same one as LaTeX uses by default, so it fits well with papers etc. On soleus, it's installed in a system-wide location, for your local computers, you can download it from my webpage, and put it into ~/Library/Fonts/ (on OS X), or ~/.fonts (on Linux). In order to check that matplotlib can find the font, you can do

from matplotlib.font_manager import findfont
findfont('Latin Modern Math')

and that should return the path of the file (or you can just try a plot and see that it comes out right). If it does not, you might have to delete the matplotlib font cache file, so that it looks for fonts again the next time it is imported. That cache is in ~/.matplotlib/fontList.cache and ~/.matplotlib/fontList.py3k.cache (on OS X), or ~/.cache/matplotlib/fontList.cache and ~/.cache/matplotlib/fontList.py3k.cache (on Linux)

Of course, feel free to modify the style file if there's anything you don't like. If you have any suggestions for improvement, also let me know.

high-resolution plots

I strongly recommend putting c.InlineBackend.figure_formats = set(['retina']) into your ~/.ipython/profile_default/ipython_config.py file, so that the figures always are produced with "double" resolution (it looks better even when not on a "retina" display, and by far better if you have a retina display).

If you are using Julia with PyPlot, I haven't found a simple way to do it, but you can add

macro pyplot_retina()
    :( begin
        using PyPlot: Figure
        using Base64: base64encode
        function Base.show(io::IO, m::MIME"text/html", f::Figure)
            buf = IOBuffer()
            show(buf,MIME("image/png"),f)
            dat = String(take!(buf));
            i = findfirst("IHDR",dat).stop
            w, h = bswap.(reinterpret(UInt32,Base.CodeUnits(dat[i+1:i+8])))
            b64png = base64encode(dat);
            html = "<img src='data:image/png;base64,$b64png' width='$(w÷2)' height='$(h÷2)'/>"
            write(io,html)
        end
        Base.showable(::MIME"text/html", f::Figure) = showable(MIME("image/png"), f)
    end )
end

to the file ~/.julia/config/startup.jl and then just call @pyplot_retina in a cell in the notebook to get the same.

nbopen

nbopen provides a script to open notebooks from the command line (on the local computer), using an existing notebook server if possible. It also provides scripts to that setup opening of .ipynb files on doubleclick (for Windows, Linux, and OS X).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment