Plotting with Matplotlib ¶
backend provides several useful interactive features, the
backend also has several features not available in other backends, including file export and animated GIF support.
To enable the
backend, we can initialize the Holoviews notebook extension:
import numpy as np import holoviews as hv
We could also use
is the default. However, it's more explicit to specify the extension we are using.
We can also switch backends on a cell-specific basis by using cell magic:
%%output backend='matplotlib' obj
During interactive exploration in the IPython Notebook, your results are always visible within the notebook itself, but you can explicitly request that any IPython cell is also exported to an external file on disk:
x = np.arange(-3.0,3.0,0.1) y = np.arange(-3.0,3.0,0.1) X,Y = np.meshgrid(x, y) def g(x,y,c): return 2*((x-y)**2/(x**2+y**2)) + np.exp(-(np.sqrt(x**2+y**2)-c)**2)
%%output filename="pulse" fig="png" holomap="gif" hv.Image(g(X,Y,2))
This mechanism can be used to provide a clear link between the steps for generating the figure, and the file on disk. You can now load the exported plot back into HoloViews, if you like, though the result would be a bit confusing due to the additional set of axes applied to the new plot:
part of the
magic above specified that the file should be saved in PNG format, which is useful for posting on web pages or editing in raster-based graphics programs. It also specified that if the object contained a
(which this particular one does not), it would be saved in GIF format, which supports animation. Because of the need for animation, objects containing a
are handled specially, as animation is not supported by the common PNG or SVG formats.
For a publication, you will usually want to select SVG format, using
, because this vector format preserves the full resolution of all text and drawing elements. SVG files can be be used in some document preparation programs directly (e.g.
), and can easily be converted using e.g.
to PDF for use with PDFLaTeX or to EMF for use with Microsoft Word. They can also be edited using Inkscape or other vector drawing programs to move graphical elements around, add arbitrary text, etc., if you need to make final tweaks before using the figures in a document. You can also embed them within other SVG figures in such a drawing program, e.g. by creating a larger figure as a template that automatically incorporates multiple SVG files you have exported separately.
PDF image export is also supported, via
backend supports animated outputs either as video (using mp4 or webm formats) or as animated GIFS. This is useful for output to web pages that users can view without needing to interact with. It can also be useful for creating descriptive pages for HoloViews constructs that require a live Python/Jupyter server rather than just a web page - see for example
Note that GIF support requires ImageMagick which is installed by default on many Linux installations and may be installed on OSX using brew . For more information on how to install ImageMagick (including Windows instructions) see the installation page .
In recent versions of matplotlib (>=2.2.0) GIF output can also be generated using
, which can be installed using
conda install pillow
pip install pillow
We can switch to 'gif' output in one of two ways, either by using the
magic setting the
option or by directly modifying the renderer with:
renderer = hv.renderer('matplotlib') renderer.holomap = 'gif'
Here we will plot an animation of the function we specified above, and also specify the
(frames per second) to control the speed of the animation:
%%opts Image [xaxis='bare', yaxis='bare' show_title=False, colorbar=True] (cmap='fire') %%output holomap='gif' fps=5 hv.HoloMap([(t, hv.Image(g(X,Y, 4 * np.sin(np.pi*t)))) for t in np.linspace(0,1,21)])
INFO:matplotlib.animation:Animation.save using <class 'matplotlib.animation.PillowWriter'>