如何在 Zeppelin 中使用 Plotly [英] How to use Plotly with Zeppelin
问题描述
我看过 zeppelin-plotly 但它似乎太复杂了.另一个让我担心的事情是它涉及修改 zeppelin 的 .war
文件,我不想因错误而破坏.
还有其他方法可以将 Plotly 与 Zeppelin 一起使用吗?
使用 %angular
解释器功能解决了这个问题.这是让它工作的完整步骤
1:安装(如果你还没有)
%sh pip install plotly
如果您有权访问,您也可以在终端上执行此操作
2:定义绘图函数
def plot(plot_dic, height=500, width=500, **kwargs):kwargs['output_type'] = 'div'plot_str = plotly.offline.plot(plot_dic, **kwargs)print('%%angular %s </div>' % (height, width, plot_str))这个自定义绘图函数使用 angular 解释器来绘制 html.它采用与 plotly.offline.plot
相同的参数加上一些额外的 div 尺寸参数(没有这些,我的结果很糟糕).像通常使用 plotly.offline.plot
一样使用它.
注意
这些情节是互动的!您不需要 iplot
,此处定义的 plot
函数也适用于 3D 交互式绘图.
完整示例
%pyspark情节导入from plotly.graph_objs import Scatter, Layoutdef plot(plot_dic, height=500, width=500, **kwargs):kwargs['output_type'] = 'div'plot_str = plotly.offline.plot(plot_dic, **kwargs)print('%%angular %s </div>' % (height, width, plot_str))阴谋({数据": [散点(x=[1, 2, 3, 4], y=[4, 5, 3, 7])],布局":布局(标题=你好世界")})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?
解决方案 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"
)
})
这篇关于如何在 Zeppelin 中使用 Plotly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文