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

Rob*_*Rob 5 python data-visualization matplotlib plotly bokeh

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

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

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

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

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

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

似乎有很多选择:

  • Jupyter(我不使用笔记本,所以我对它们不太熟悉,但使用 mybinder 我认为这很容易做到)
  • 情节
  • 散景服务器
  • pyviz.org(这是非常有趣的一个,但同样,关于如何实现这一点似乎有很多选择)

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

Jor*_*ris 3

确实有很多选择,我不确定什么是最好的,但我经常使用散景并且对此感到很高兴。下面的示例可以帮助您入门。要启动此功能,请在保存脚本的目录中打开 cmd 并运行“bokehserve script.py --show --allow-websocket-origin=*”。

from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.models.widgets import Slider
from bokeh.models import Row,ColumnDataSource

#create the starting data
x=[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 slope
intercept=0 #set the line to go through 0, you can change this later
y= [slope*i + intercept  for i in x]#create the y data via a list comprehension

# create a plot
fig=figure() #create a figure
source=ColumnDataSource(dict(x=x, y=y)) #the data destined for the figure
fig.circle(x,y_noise)#add some datapoints to the plot
fig.line('x','y',source=source,color='red')#add a line to the figure

#create a slider and update the graph source data when it changes
def 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 eachother
curdoc().add_root(layout)#serve it via "bokeh serve slider.py --show --allow-websocket-origin=*"
Run Code Online (Sandbox Code Playgroud)

allow-websocket-origin=* 是为了允许其他用户连接到服务器并查看图表。http 将为http://yourPCservername:5006/(5006 是默认的散景端口)。如果您不想通过 PC 提供服务,您可以订阅 Heroku 之类的云服务:example