Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Plotly with Zeppelin

I've seen zeppelin-plotly but it seems too complicated. The other things that worries me is that it involves modifying zeppelin's .war file and I don't want to break things by error.

Is there another way to use Plotly with Zeppelin?

like image 670
Cristian Garcia Avatar asked May 13 '16 20:05

Cristian Garcia


1 Answers

Figured it out using the %angular interpreter feature. Here are the full steps to get it working

1: Install plotly (if you haven't)

%sh pip install plotly

You can also do this on the terminal if you have access to it

2: Define a plot function

def plot(plot_dic, height=500, width=500, **kwargs):
    kwargs['output_type'] = 'div'
    plot_str = plotly.offline.plot(plot_dic, **kwargs)
    print('%%angular <div style="height: %ipx; width: %spx"> %s </div>' % (height, width, plot_str))

This custom plot funtion uses the angular interpreter to plot html. It take the same parameters as plotly.offline.plot plus some extra parameters for the div's dimensions (I had bad results without these). Use it as you normally would use plotly.offline.plot.

Note

These plots are interactive! You don't need iplot, the plot function defined here works for 3D interactive plots as well.

Full Example

%pyspark

import plotly
from plotly.graph_objs import Scatter, Layout


def plot(plot_dic, height=500, width=500, **kwargs):
    kwargs['output_type'] = 'div'
    plot_str = plotly.offline.plot(plot_dic, **kwargs)
    print('%%angular <div style="height: %ipx; width: %spx"> %s </div>' % (height, width, plot_str))


plot({
    "data": [
        Scatter(x=[1, 2, 3, 4], y=[4, 5, 3, 7])
    ],
    "layout": Layout(
        title="hello world"
    )
})
like image 168
Cristian Garcia Avatar answered Sep 25 '22 14:09

Cristian Garcia