Interactive data visualizations#
Jupyter Notebook has support for many kinds of interactive outputs, including the ipywidgets ecosystem as well as many interactive visualization libraries. These are supported in Jupyter Book, with the right configuration. This page has a few common examples.
First off, we’ll download a little bit of data and show its structure:
import plotly.express as px
data = px.data.iris()
data.head()
sepal_length | sepal_width | petal_length | petal_width | species | species_id | |
---|---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa | 1 |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa | 1 |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa | 1 |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa | 1 |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa | 1 |
Altair#
Interactive outputs will work under the assumption that the outputs they produce have
self-contained HTML that works without requiring any external dependencies to load.
See the Altair
installation instructions
to get set up with Altair. Below is some example output.
import altair as alt
alt.Chart(data=data).mark_point().encode(
x="sepal_width",
y="sepal_length",
color="species",
size='sepal_length'
)
Plotly#
Plotly is another interactive plotting library that provides a high-level API for visualization. See the Plotly JupyterLab documentation to get started with Plotly in the notebook.
Below is some example output.
Important
For these plots to show, it may be necessary to load require.js
, in your _config.yml
:
sphinx:
config:
html_js_files:
- https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js
Important
Including plotly plots in a Jupyter Book page is currently not compatible with the dollarmath syntax extension (mathematical notation written between two “$” characters). If you are trying to include both plotly plots and mathematical notation within the same page, and finding that plotly plots are not being rendered, this may be the cause. Try removing all use of the dollarmath syntax within the page and rebuilding the book.
import plotly.io as pio
import plotly.express as px
import plotly.offline as py
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", size="sepal_length")
fig
Important
Since jupyter-book version 0.14, showing plotly figures will generate a warning when building the book. You can suppress this warning by adding the following to your _config.yml
:
sphinx:
config:
suppress_warnings: ["mystnb.unknown_mime_type"]
Bokeh#
Bokeh provides several options for interactive visualizations, and is part of the PyViz ecosystem. See the Bokeh with Jupyter documentation to get started.
Below is some example output. First we’ll initialized Bokeh with output_notebook()
.
This needs to be in a separate cell to give the JavaScript time to load.
Now we’ll make our plot.
p = figure()
p.circle(data["sepal_width"], data["sepal_length"], fill_color=data["species"], size=data["sepal_length"])
show(p)
ipywidgets#
You may also run code for Jupyter Widgets in your document, and the interactive HTML outputs will embed themselves in your site. See the ipywidgets documentation for how to get set up in your own environment.
Here are some simple widget elements rendered below.
import ipywidgets as widgets
widgets.IntSlider(
value=7,
min=0,
max=10,
step=1,
description='Test:',
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='d'
)
tab_contents = ['P0', 'P1', 'P2', 'P3', 'P4']
children = [widgets.Text(description=name) for name in tab_contents]
tab = widgets.Tab()
tab.children = children
for ii in range(len(children)):
tab.set_title(ii, f"tab_{ii}")
tab
You can find a list of existing Jupyter Widgets in the jupyter-widgets documentation.