猿问

如何最好地使用 python 创建一个简单的、交互式的、可共享的绘图?

我希望有人能指出我正确的方向。python datavis 领域现在已经变得巨大,并且有很多选择,我有点迷失了实现这一目标的最佳方法是什么。

我有一个 xarray 数据集(但它很容易是一个 Pandas 数据框或一个 numpy 数组列表)。

我有 3 列,A、B 和 C。它们包含 40 个数据点。

我想绘制 A vs B + scale*C 的散点图,其中比例是从交互式滑块确定的。

更高级的版本会有一个下拉列表,您可以在其中选择一组不同的 3 列,但稍后我会担心这一点。

对所有这些的警告是,我希望它在线并且可以交互以供其他人使用。

似乎有很多选择:

  • Jupyter(我不使用笔记本,所以我对它们不太熟悉,但使用 mybinder 我认为这很容易做到)

  • 情节

  • 散景服务器

  • pyviz.org(这是非常有趣的一个,但同样,关于如何实现这一点似乎有很多选择)

任何想法或建议将不胜感激。


UYOU
浏览 130回答 1
1回答

慕容3067478

确实有很多选择,我不确定什么是最好的,但我经常使用散景并对此感到高兴。下面的示例可以帮助您入门。要启动它,请在保存脚本的目录中打开一个 cmd 并运行“bokeh serve script.py --show --allow-websocket-origin=*”。from bokeh.plotting import figurefrom bokeh.io import curdocfrom bokeh.models.widgets import Sliderfrom bokeh.models import Row,ColumnDataSource#create the starting datax=[0,1,2,3,4,5,6,7,8]y_noise=[1,2,2.5,3,3.5,6,5,7,8]slope=1 #set the starting value of the slopeintercept=0 #set the line to go through 0, you can change this latery= [slope*i + intercept  for i in x]#create the y data via a list comprehension# create a plotfig=figure() #create a figuresource=ColumnDataSource(dict(x=x, y=y)) #the data destined for the figurefig.circle(x,y_noise)#add some datapoints to the plotfig.line('x','y',source=source,color='red')#add a line to the figure#create a slider and update the graph source data when it changesdef updateSlope(attrname, old, new):    print(str(new)+" is the new slider value")    y = [float(new)*i + intercept  for i in x]    source.data = dict(x=x, y=y)   slider = Slider(title="slope", value=slope, start=0.0, end=2.0,step=0.1)slider.on_change('value', updateSlope)layout=Row(fig,slider)#put figure and slider next to eachothercurdoc().add_root(layout)#serve it via "bokeh serve slider.py --show --allow-websocket-origin=*"allow-websocket-origin=* 是允许其他用户访问服务器并查看图表。http 将是http://yourPCservername:5006/(5006 是默认的散景端口)。如果您不想从您的 PC 上提供服务,您可以订阅像 Heroku 这样的云服务:example。
随时随地看视频慕课网APP

相关分类

Python
我要回答