Performing Multimedia and Graphic Integration with IPython Notebook for Data Science
Data scientists love pictures. Pictures say a lot of things that words can’t say (or at least they do it with far less effort). IPython Notebook is both a coding platform and a presentation platform. You may be surprised at just what you can do with it. Check out this brief overview of some of the more interesting features.
Embedding plots and other images
At some point, you might have spotted an IPython Notebook with multimedia or graphics embedded into it and wondered why you didn’t see the same effects in your own files. Fortunately, you can perform some more magic by using the %matplotlib magic function. The possible values for this function are: ‘gtk’, ‘gtk3’, ‘inline’, ‘nbagg’, ‘osx’, ‘qt’, ‘qt4’, ‘qt5’, ‘tk’, and ‘wx’, each of which defines a different plotting backend (the code used to actually render the plot) used to present information onscreen.
When you run %matplotlib inline, any plots you create appear as part of the document. When you try this technique, you get the output shown here.
Loading examples from online sites
Because some examples you see online can be hard to understand unless you have them loaded on your own system, you should also keep the %load magic function in mind. All you need is the URL of an example you want to see on your system. For example, try %load http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/integral_demo.py. When you click Run Cell, IPython Notebook loads the example directly below the cell. You can then run the example and see the output from it on your own system.
Obtaining online graphics and multimedia
A lot of the functionality required to perform special multimedia and graphics processing appears within IPython.display. By importing a required class, you can perform tasks such as embedding images into your notebook. Here’s an example of embedding one of the pictures from an author’s blog into the notebook:
from IPython.display import Image Embed = Image( ‘http://blog.johnmuellerbooks.com/’ + ‘wp-content/uploads/2015/04/Layer-Hens.jpg’) Embed
The code begins by importing the required class, Image, and then using features from it to first define what to embed and then actually embed the image. The output you see from this example can be seen here.
If you expect an image to change over time, you might want to create a link to it instead of embedding it. You must refresh a link because the content in the notebook is only a reference rather than the actual image. However, as the image changes, you see the change in your notebook as well. To accomplish this task, you use SoftLinked = Image(url=‘http://blog.johnmuellerbooks.com/wp-content/uploads/2015/04/Layer-Hens.jpg’) instead of Embed.
When working with embedded images on a regular basis, you might want to set the form in which the images are embedded. For example, you may prefer to embed them as PDFs. To perform this task, you use code similar to this:
from IPython.display import set_matplotlib_formats set_matplotlib_formats(‘pdf’, ‘svg’)
You have access to a wide number of formats when working with a notebook. The commonly supported formats are ‘png’, ‘retina’, ‘jpeg’, ‘svg’, and ‘pdf’.
The IPython display system is nothing short of amazing, and this example doesn’t even begin to tap the surface for you. For example, you can import a YouTube video and place it directly into your notebook as part of your presentation if you want.