How to use Plotly with Zeppelin

主宰稳场 提交于 2021-02-18 00:53:51

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/37219069/how-to-use-plotly-with-zeppelin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!