I am forever interested in exploring and visualizing data. As anybody that have ever used matplotlib
knows, sometimes it just isn't interactive enough. You almost have to know what you are looking for to use it right. I have been actively loooking for a plotting package that helps with that. Recently I introduced us to bokeh
, but this week I will be introducing plotly
.
The name plotly
may be familiar with some of you. That is because it is an online company that does data plotting. It is free to all users, so long as the users are okay with their plots and data being public. However, their service-based business model allows paying users to have private data. But why would I introduce an online based plotting service?
plotly
has a Python & R API. That means we can code the plots we want and house the data on our own machines. Furthermore, this introduction will show you just how well plotly
allows users to share interactive graphs.
Below, I will import the plotly.offline
package in conjunction with the py.init_notebook_mode()
method. This allows me to circumvent the API key requirement needed (which requires the user to sign-up for a free and/or paid plot.ly account).
import plotly.offline as py
py.init_notebook_mode()
import plotly.graph_objs as go
import numpy as np
plotly
's plotting language is similar to the "Grammer of Graphics" used in ggplot2
. By similar, I mean that you state all of the style of the graph before you plot it.
Here I am just going to make some random data for us to work with.
n = 1000
x = np.linspace(0,1,n)
y1 = np.random.randn(n)+10
y2 = np.random.randn(n)
y3 = np.random.randn(n)-10
Now we can start plotting. I am just going to show a single plot to show that unlike bokeh
, I can directly plot into a notebook.
plt1 = go.Scatter(
x = x,
y = y1,
mode = 'lines+markers',
)
data = [plt1]
py.iplot(data)
Hopefully your mouse can easily hover over the data points to get the data. Now, let's see something a little more complex.
trace1 = go.Scatter(
x = x,
y = y1,
mode = 'lines',
name = 'first'
)
trace2 = go.Scatter(
x = x,
y = y2,
mode = 'lines+markers',
name = 'second'
)
trace3 = go.Scatter(
x = x,
y = y3,
mode = 'markers',
name = 'third'
)
data = [trace1, trace2, trace3]
py.iplot(data)
Now you should see that if you hover your mouse over the points, it will track across all three plots! Furthermore, *Click* on one of the names in the legend. Did you just see that?! Now you can select which data tracks are visible, both quickly and easily. You can even click & drag like the UCSC Genome Browser to highlight a specific region. You can even select a single data point with your mouse. Lastly, if you want to save what you are looking at, just clock the camera button to save as a .png file.
You can go a lot further with this library. You can add slider bars, data selection buttons, & mouse interaction. However, some of these examples require