A recurrent limitation of visualization tools like matplotlib or seaborn is that they produce only static plots. Once the plot is displayed on a jupyther notebook, a html page or other supports, you cannot zoom in/out or read the values of the data points for example, without modifying the original code. One option would be be to develop your own code for interactive plots. Another option is to use an existing tool.
Plotly is an online/offline analytics and data visualization tool that enables to generate interactive plots. In the offline mode, you can use the API, without having to register. Plotly has free libraries for Python, R, MATLAB, Perl, Julia, Arduino, and REST. There is also a Chrome app, available here.
I will not go into details about what is or is not possible with plotly (you can check their website for that!). Instead, I will show you how to start and a few examples of interactive plots to be embedded in Jupyther Notebooks and in html pages. I might update this post later on, after learning more about the really cool stuffs that plotly can deliver.
##1. plotpy in a Jupyther notebook
#1.1. Installation
First thing first, the installation of the Plotly’s package. It is as easy as:
$ sudo pip install plotly
1.2. Interactive plots in Jupyther notebook
Let’s dive in, and generate our first interactive plot in a jupyther notebook.
####1.2.1. Exemple 1: scatter plot of a single dataset in a Jupyther notebook
from plotly.offline import init_notebook_mode, iplot | |
from plotly.graph_objs import * | |
init_notebook_mode() | |
#Generate some data | |
data_x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] | |
data_y = [13, 13.2, 11.3, 12, 8.4, 9, 9.5, 6.5, 6.8, 4.3, 3.2, 2.8, 1.1, 0] | |
iplot([Scatter(x=data_x, y=data_y)]) |
####1.2.2. Exemple 2: scatter plot of 2 datasets in a Jupyther notebook
from plotly.offline import init_notebook_mode, iplot | |
from plotly.graph_objs import * | |
init_notebook_mode() | |
trace0 = Scatter( | |
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], | |
y = [13, 13.2, 11.3, 12, 8.4, 9, 9.5, 6.5, 6.8, 4.3, 3.2, 2.8, 1.1, 0] | |
) | |
trace1 = Scatter( | |
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], | |
y = [1, 1.3, 13, 16, 12.5, 11, 11.5, 8.5, 8.8, 6.3, 5.2, 4.8, 3.1, 2] | |
) | |
data = Data([trace0, trace1]) | |
iplot(data, filename = 'basic-line') |
####1.2.3. Exemple 3: BAR plot in a Jupyther notebook
from plotly.offline import init_notebook_mode, iplot | |
from plotly.graph_objs import * | |
init_notebook_mode() | |
trace1 = Bar( | |
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], | |
y = [13, 13.2, 11.3, 12, 8.4, 9, 9.5, 6.5, 6.8, 4.3, 3.2, 2.8, 1.1, 0] | |
) | |
data = [trace1] | |
iplot(data, filename='bar-plot') |
###2. pyplot in html using javascript
-
(1) In the header of the html page, we call plotly javascript file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters<header> <script type="text/javascript" src="https://cdn.plot.ly/plotly-latest.min.js"> </script> </header> -
(2) we create a
<div>
in which to display the plot:This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters<div id="show_plot" style="width:600px; height:250px;"> </div> -
(3) Finally, we generate the plot
<script> | |
myplot = document.getElementById('show_plot'); | |
Plotly.plot( myplot, [{ | |
x: [1, 2, 3, 4, 5], | |
y: [1, 2, 4, 8, 16] }], { | |
margin: { t:0 } } ); | |
</script> |
The plot will be displayed in the <div id='show_plot'></div>