# Plotting with Matplotlib ¶

The default plotting backend for HoloViews is based on Matplotlib . There is also a Bokeh backend - see the Plotting with Bokeh user guide.

While the  'bokeh'  backend provides several useful interactive features, the  'matplotlib'  backend also has several features not available in other backends, including file export and animated GIF support.

To enable the  'matplotlib'  backend, we can initialize the Holoviews notebook extension:

In [1]:
import numpy as np
import holoviews as hv

In [2]:
hv.extension('matplotlib')


We could also use  hv.extension()  because  'matplotlib'  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


## Exporting specific files ¶

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:

In [3]:
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)

In [4]:
%%output filename="pulse" fig="png" holomap="gif"
hv.Image(g(X,Y,2))

Out[4]:

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:

In [5]:
hv.RGB.load_image('pulse.png')

Out[5]:

The  fig="png"  part of the  %%output  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  HoloMap  (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  HoloMap  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  fig="svg"  , 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. LibreOffice ), and can easily be converted using e.g. Inkscape 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  fig="pdf"  .

## Animation support ¶

The  'matplotlib'  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 DynamicMap .

### GIF ¶

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 pillow , which can be installed using  conda install pillow  or  pip install pillow  .

We can switch to 'gif' output in one of two ways, either by using the  %%output  magic setting the  holomap='gif'  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  fps  (frames per second) to control the speed of the animation:

In [6]:
%%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'>

Out[6]: