5 Interesting Jupyter Interactive Visualization Extensions | by Cornellius Yudha Wijaya | Towards Data Science

Visualization is an important part of data analysis. Without visualization, we would have a hard time summarizing the information from our data. However, there are times we want to have a dynamic visualization to ease our analysis activity.
Luckily, Jupyter Notebook has many visualization extensions we could use to create an interactive visualization from your Notebook. What are these extensions, and how can you use them? Let’s explore five interesting Jupyter Interactive Visualization Extensions.
The Jupyter extensions ipywidgets is an interactive HTML Jupyter widgets notebook. The package lets you create a widget to control your data via interactive GUI such as slider, checkboxes, text field, and many more. It is pretty helpful if we want to create a GUI-based visualization on Jupyter Notebook manually.
To start, we need to install the extensions package first.
pip install ipywidgets
Using ipywidgets, we could customize the Jupyter Notebook widget. Let’s explore the basic widgets available.
from ipywidgets import interact#Basic ipywidgets interact would generate UI from the functiondef f(x):    return x
The ipywidgets extensions interact with the base to generate UI from the function. The generated UI would depend on the data type we are passed. Let’s try to pass integer data.
interact(f, x=10)
Passing integer values would return slider UI. Next, let’s use the Boolean data.
interact(f, x=True)
The Boolean value would return the checkbox. Finally, let’s use string value.
interact(f, x='hi')
All the UI was great, but how to apply it to the visualization? We need to create our function to return all the interactive visualization. Let’s use the Seaborn package to combine it with ipywidgets.
import seaborn as snsimport matplotlib.pyplot as pltmpg = sns.load_dataset('mpg')def f(x):    img = sns.countplot(mpg[mpg['mpg']<x]['origin'])     plt.title('Origin Countplot mpg = {}'.format(x))    return img
When we move the slide bar, we will execute the function to return the count plot of the ‘origin’ when we filter it with the slider ipywidget. If you want to read more about what ipywidgets could do, you can read it all here.
Pandas-Profiling is an exploratory package that produces interactive HTML reports on our Jupyter Notebook. The basic report from Pandas-Profiling is shown in the GIF below.
As we can see from the GIF, the report produced is an interactive HTML report within your Jupyter Notebook. How do you generate this? Let’s try it out with sample data.
First, we need to install the Pandas-Profiling package.
#Installing via pippip install -U pandas-profiling[notebook]#Enable the widget extension in Jupyterjupyter nbextension enable --py widgetsnbextension#or if you prefer via Condaconda env create -n pandas-profilingconda activate pandas-profilingconda install -c conda-forge pandas-profiling#or if you prefer installing directly from the sourcepip install https://github.com/pandas-profiling/pandas-profiling/archive/master.zip#in any case, if the code raise an error, it probably need permission from user. To do that, add --user in the end of the line.
After installing the package, we are ready to generate the report. Let’s use the mpg sample data we had previously.
#Importing the functionfrom pandas_profiling import ProfileReport#Generate the report. We would use the mpg dataset as sample, title parameter for naming our report, and explorative parameter set to True for Deeper exploration.profile = ProfileReport(mpg, title='MPG Pandas Profiling Report', explorative = True)profile
After some time, the report would show up, and you would explore the HTML.
The report selection is shown in the image above. You could try out all the options to explore the data. If you want to dig the package deeper, you can read my previous articles below.
towardsdatascience.com
towardsdatascience.com
Jupyter-Matplotlib or ipympl is a Jupyter Notebook extension to enable interactive Matplotlib visualization exploration within your Jupyter Notebook.
The exploration is simple yet powerful if we need a quick visualization with matplotlib. Let’s try to install the package first.
pip install ipympl
When you finish installing the package, you need to enable the ipympl extension in your Jupyter Notebook by using the following magic command: %matplotlib widget .
Let’s try the matplotlib extension with sample data.
%matplotlib widgetimport matplotlib.pyplot as pltimport numpy as npfig = plt.figure()plt.plot(np.sin(np.linspace(0, 20, 100)));
As you can see in the image above, our matplotlib visualization becomes an interactive visualization where you can pan and zoom within the plot.
We can pass another kind of matplotlib plot with our dataset. Let’s try it with our previous mpg dataset.
fig = plt.figure()plt.scatter(mpg['mpg'], mpg['horsepower'])
As long as the visualization is based on the matplotlib package, we could pass the interactive widget within the plot. That is why it would work with another kind of matplotlib visualization.
You could implement other widgets using ipympl that are similar to the ipywidgets; however, I feel the ipywidgets package has covered many of the functions. If you are interested in the ipympl widgets, you could explore the example.
If you want to have an interactive Pandas dataframe table, you might want to install itables package extensions. This package would allow you to have a simple exploration capability in the data frame.
How do you enable this extension? Let’s install the package first.
pip install itables
When you have finished installing the package, you only need to run the following code.
from itables import init_notebook_modeinit_notebook_mode(all_interactive=True)
Your data frame would automatically have interactive extensions similar to the GIF above. If you want to display only one series or data frame, you could use the following code.
from itables import showshow(mpg[['mpg', 'name']])
Do you want to have an interactive canvas you could play on your Jupyter Notebook? If so, then you should try the ipycanvas package extension. These extensions allow you to create a drawable canvas within your Jupyter Notebook with drawing style. Let’s install the package first.
pip install ipycanvas orjson
After installing the package, let’s try to initiate a hand-drawing canvas from the notebook example.
from ipywidgets import Image,ColorPicker, IntSlider, link, AppLayout, HBox, RoughCanvas, hold_canvaswidth = 800height = 800canvas = RoughCanvas(width=width, height=height)drawing = Falseposition = Noneshape = []def on_mouse_down(x, y):    global drawing    global position    global shapedrawing = True    position = (x, y)    shape = [position]def on_mouse_move(x, y):    global drawing    global position    global shapeif not drawing:        returnwith hold_canvas(canvas):        canvas.stroke_line(position[0], position[1], x, y)position = (x, y)shape.append(position)def on_mouse_up(x, y):    global drawing    global position    global shapedrawing = False        with hold_canvas(canvas):        canvas.stroke_line(position[0], position[1], x, y)        canvas.fill_polygon(shape)shape = []canvas.on_mouse_down(on_mouse_down)canvas.on_mouse_move(on_mouse_move)canvas.on_mouse_up(on_mouse_up)canvas.stroke_style = '#749cb8'picker = ColorPicker(description='Color:', value='#749cb8')link((picker, 'value'), (canvas, 'stroke_style'))link((picker, 'value'), (canvas, 'fill_style'))HBox((canvas, picker))
In the image above I am playing around with the Canvas I have created with the above code. You could try to click the canvas and draw something interesting.
If you still want to look at what ipycanvas could do, you can read the whole documentation here.
Jupyter Notebook is one of the most used IDE by data scientists for the versatility it offers. Activity such as data visualization is often done in the Notebook as well. To help with this activity, I am outlining five interesting Jupyter extensions interactive visualization. They are:
ipywidgetspandas-profilingipymplitablesipycanvas
ipywidgets
pandas-profiling
ipympl
itables
ipycanvas
I hope it helps!
Visit me on my LinkedIn or Twitter.
If you are not subscribed as a Medium Member, please consider subscribing through my referral.