#!/usr/bin/env python # coding: utf-8 # > This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python. # # # 6.3. Creating interactive Web visualizations with Bokeh # 1. Let's import NumPy and Bokeh. We need to call `output_notebook` in order to tell Bokeh to render plots in the IPython notebook. # In[3]: import numpy as np import bokeh.plotting as bkh bkh.output_notebook() # 2. Let's create some random data. # In[4]: x = np.linspace(0., 1., 100) y = np.cumsum(np.random.randn(100)) # 3. Let's draw a curve. # In[5]: #bkh.line(x, y, line_width=5) #bkh.show() p = bkh.figure() p.line(x=x, y=y) bkh.show(p) # An interactive plot is rendered in the notebook. We can pan and zoom by clicking on the buttons above the plot. # 4. Let's move to another example. We first load a sample dataset (*Iris flowers*). We also generate some colors based on the species of the flowers. # In[6]: from bokeh.sampledata.iris import flowers colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'} flowers['color'] = flowers['species'].map(lambda x: colormap[x]) # 5. Now, we render an interactive scatter plot. # In[9]: p = bkh.figure() p.scatter(flowers["petal_length"], flowers["petal_width"], color=flowers["color"], fill_alpha=0.25, size=10,) bkh.show(p) # > You'll find all the explanations, figures, references, and much more in the book (to be released later this summer). # # > [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages).