Skip to content

Instantly share code, notes, and snippets.

Created August 30, 2014 11:13
Show Gist options
  • Save anonymous/b282008c3e3e756b37a3 to your computer and use it in GitHub Desktop.
Save anonymous/b282008c3e3e756b37a3 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:54cc72b5195d662160c3a894b011054ed2c9ea0a03cec016fde846d1938e4d05"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"internals": {
"slide_type": "subslide"
},
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Matplotlib's new nbagg backend"
]
},
{
"cell_type": "markdown",
"metadata": {
"internals": {
"frag_number": 1
},
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
" * Interactive matplotlib figures in a live IPython notebook\n",
" * Not a JS library - the figure is rendered on the server and passed as as PNG to the browser."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib\n",
"matplotlib.use('nbagg')\n",
"import matplotlib.pyplot as plt"
],
"language": "python",
"metadata": {
"internals": {
"frag_helper": "fragment_end",
"frag_number": 2,
"slide_helper": "subslide_end"
},
"slide_helper": "slide_end",
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"\n",
"x = 100 + 15 * np.random.randn(10000)\n",
"\n",
"n, bins, patches = plt.hist(x, 50, facecolor='green', alpha=0.5)\n",
"plt.show()"
],
"language": "python",
"metadata": {
"internals": {
"frag_helper": "fragment_end",
"frag_number": 2,
"slide_helper": "subslide_end",
"slide_type": "subslide"
},
"slide_helper": "slide_end",
"slideshow": {
"slide_type": "slide"
}
},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"internals": {
"frag_helper": "fragment_end",
"frag_number": 2,
"slide_type": "subslide"
},
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"The IPython widgets can be used to interact with the figure:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.html.widgets import interact\n",
"from IPython.display import display\n",
"import numpy as np\n",
"\n",
"fig = plt.figure()\n",
"ax = plt.axes()\n",
"\n",
"line, = ax.plot([0, 2*np.pi], [-1, 1])\n",
"\n",
"def update_line(n_peaks=2):\n",
" x = np.linspace(0, 2*np.pi, 1000)\n",
" line.set_data(x, np.sin(x * n_peaks))\n",
" fig.canvas.draw()\n",
"\n",
"update_line(2)\n",
"\n",
"plt.show()"
],
"language": "python",
"metadata": {
"internals": {
"frag_helper": "fragment_end",
"frag_number": 5
},
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"interact(update_line, n_peaks=(1, 20));"
],
"language": "python",
"metadata": {
"internals": {
"frag_helper": "fragment_end",
"frag_number": 6
},
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"internals": {
"frag_helper": "fragment_end",
"frag_number": 6,
"slide_helper": "subslide_end"
},
"slide_helper": "slide_end",
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"<br><br><br><br><br><br><br><br><br><br>"
]
},
{
"cell_type": "markdown",
"metadata": {
"internals": {
"frag_helper": "fragment_end",
"frag_number": 6,
"slide_type": "subslide"
},
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"In case you were wondering (of course you were) - XKCD still works..."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"plt.figure()\n",
"plt.axes([0, 0, 1, 1], aspect='equal')\n",
"with plt.xkcd():\n",
"\n",
" # The slices will be ordered and plotted counter-clockwise.\n",
" labels = ['Fraction of this image\\nwhich is white',\n",
" 'Fraction of this image\\nwhich is black']\n",
" fracs = [70, 30]\n",
" explode=(0, 0.05)\n",
"\n",
" plt.pie(fracs, labels=labels, startangle=90, colors=['white', 'black'],\n",
" labeldistance=0.3)\n",
" \n",
"plt.show()"
],
"language": "python",
"metadata": {
"internals": {
"frag_helper": "fragment_end",
"frag_number": 9,
"slide_helper": "subslide_end"
},
"slide_helper": "slide_end",
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"internals": {
"frag_helper": "fragment_end",
"frag_number": 9,
"slide_type": "subslide"
},
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"**Everything** you can do with matplotlib in interactive backends is possible with the nbagg backend.\n",
"\n",
"Here I make use of cartopy to create a map:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import cartopy.crs as ccrs\n",
"\n",
"plt.figure(figsize=(10, 4))\n",
"ax = plt.axes(projection=ccrs.InterruptedGoodeHomolosine())\n",
"ax.coastlines('50m')\n",
"plt.show()"
],
"language": "python",
"metadata": {
"internals": {
"frag_helper": "fragment_end",
"frag_number": 9,
"slide_helper": "subslide_end"
},
"slide_helper": "subslide_end",
"slideshow": {
"slide_type": "-"
}
},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"internals": {
"frag_helper": "fragment_end",
"frag_number": 9,
"slide_type": "subslide"
},
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Or, for interactive figures, with interactive features being downloaded on the fly:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import cartopy.crs as ccrs\n",
"\n",
"plt.figure(figsize=(8, 8))\n",
"ax = plt.axes([0, 0, 1, 1], projection=ccrs.PlateCarree())\n",
"ax.coastlines('50m')\n",
"\n",
"ax.add_wmts('http://map1c.vis.earthdata.nasa.gov/wmts-geo/wmts.cgi',\n",
" 'MODIS_Terra_CorrectedReflectance_TrueColor')\n",
"plt.show()"
],
"language": "python",
"metadata": {
"internals": {
"frag_helper": "fragment_end",
"frag_number": 13,
"slide_helper": "subslide_end"
},
"slide_helper": "slide_end",
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"internals": {
"frag_helper": "fragment_end",
"frag_number": 13,
"slide_helper": "subslide_end",
"slide_type": "subslide"
},
"slide_helper": "slide_end",
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Still some rough edges:\n",
"\n",
" * Better user experience\n",
" * Fully integrated with the IPython widget framework\n",
" * An event loop\n",
" * Better JS code\n"
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment