Skip to content

Commit

Permalink
Start matplotlib guide (#1658)
Browse files Browse the repository at this point in the history
* follow intro style of bokeh guide
* copy some info from Exporting guide
* add new animated GIF example
  • Loading branch information
jordansamuels authored and jlstevens committed Jul 7, 2017
1 parent a3e669d commit 2254d17
Showing 1 changed file with 188 additions and 0 deletions.
188 changes: 188 additions & 0 deletions examples/user_guide/Plotting_with_Matplotlib.ipynb
@@ -0,0 +1,188 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotting with Matplotlib"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The default plotting backend for HoloViews is based on [Matplotlib](http://matplotlib.org). There is also a [Bokeh](http://bokeh.pydata.org) backend - see the [Plotting with Bokeh](Plotting_with_Bokeh.ipynb) user guide.\n",
"\n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To enable the ``'matplotlib'`` backend, we can initialize the Holoviews notebook extension:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import holoviews as hv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"hv.extension('matplotlib')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We could also use `hv.extension()` because ``'matplotlib'`` is the default. However, it's more explicit to specify the extension we are using."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also switch backends on a cell-specific basis by using cell magic:\n",
"```python\n",
"%%output backend='matplotlib'\n",
"obj\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exporting specific files"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = np.arange(-3.0,3.0,0.1)\n",
"y = np.arange(-3.0,3.0,0.1)\n",
"X,Y = np.meshgrid(x, y) \n",
"def g(x,y,c):\n",
" return 2*((x-y)**2/(x**2+y**2)) + np.exp(-(np.sqrt(x**2+y**2)-c)**2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%output filename=\"pulse\" fig=\"png\" holomap=\"gif\"\n",
"hv.Image(g(X,Y,2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"hv.RGB.load_image('pulse.png')"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"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.\n",
"\n",
"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](http://www.libreoffice.org/)), and can easily be converted using e.g. [Inkscape](https://inkscape.org) 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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"PDF image export is also supported, via `fig=\"pdf\"`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Animated GIF support"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The ``'matplotlib'`` backend supports animated GIF output. 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](../reference/containers/matplotlib/DynamicMap.ipynb).\n",
"\n",
"Note that GIF support requires [ImageMagick](http://www.imagemagick.org) which is installed by default on many Linux installations and may be installed on OSX using [brew](http://brew.sh) . For more information on how to install ImageMagick (including Windows instructions) see the [installation page](http://www.imagemagick.org/script/binary-releases.php)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%opts Image [xaxis='bare', yaxis='bare' show_title=False, colorbar=True] (cmap='fire')\n",
"%%output holomap='gif'\n",
"\n",
"hv.HoloMap([(t, hv.Image(g(X,Y, 4 * np.sin(np.pi*t)))) for t in np.linspace(0,1,21)]) "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 2254d17

Please sign in to comment.