Cristian Garcia
Cristian Garcia

Reputation: 9859

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?

Upvotes: 8

Views: 7221

Answers (1)

Cristian Garcia
Cristian Garcia

Reputation: 9859

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"
    )
})

Upvotes: 12

Related Questions