Thursday, December 12, 2013

Multiple axes and subplots in Plotly

Some time ago we have seen how to visualize 2D histograms with Plotly and in this post we will see how to use one of the mostin interesting new features introduced by the Plotly guys: multiple axes into subplots. This features makes us able to couple subplots, so when you zoom or pan in one subplot, it zooms and pans in the other subplots. Just like the graphs produced by D3.
Here's how to plot a subplots matrix where each cell is a scatterplot between the features of the Iris dataset that we already used here. The first thing we need to do is to convert the data in the format required by the Plotly API:
```from sklearn.datasets import load_iris

attr = [f.replace(' (cm)', '') for f in iris.feature_names]
colors = {'setosa': 'rgb(31, 119, 180)',
'versicolor': 'rgb(255, 127, 14)',
'virginica': 'rgb(44, 160, 44)'}

data = []
for i in range(4):
for j in range(4):
for t,flower in enumerate(iris.target_names):
data.append({"name": flower,
"x": iris.data[iris.target == t,i],
"y": iris.data[iris.target == t,j],
"type":"scatter", "mode":"markers",
'marker': {'color': colors[flower],
'opacity':0.7},
"xaxis": "x"+(str(i) if i!=0 else ''),
"yaxis": "y"+(str(j) if j!=0 else '')})
```
Then, we create a layout to adjust the look and feel:
```d = 0.04; # padding
dms = [[i*d+i*(1-3*d)/4,i*d+((i+1)*(1-3*d)/4)] for i in range(4)]

layout = {
"xaxis":{"domain":dms[0], "title":attr[0],
'zeroline':False,'showline':False},
"yaxis":{"domain":dms[0], "title":attr[0],
'zeroline':False,'showline':False},
"xaxis1":{"domain":dms[1], "title":attr[1],
'zeroline':False,'showline':False},
"yaxis1":{"domain":dms[1], "title":attr[1],
'zeroline':False,'showline':False},
"xaxis2":{"domain":dms[2], "title":attr[2],
'zeroline':False,'showline':False},
"yaxis2":{"domain":dms[2], "title":attr[2],
'zeroline':False,'showline':False},
"xaxis3":{"domain":dms[3], "title":attr[3],
'zeroline':False,'showline':False},
"yaxis3":{"domain":dms[3], "title":attr[3],
'zeroline':False,'showline':False},
"showlegend":False,
"width": 500,
"height": 550,
"title":"Iris flower data set",
"titlefont":{'color':'rgb(67,67,67)', 'size': 20}
}
```
Finally, we import the plotly module (see this page for more details about the installation) and we are read to invoke the Plotly remote service:
```import plotly