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

2024-04-27 17:35:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望有人能给我指出正确的方向。pythondatavis的前景现在变得巨大了,有太多的选项,我有点迷茫于什么是实现这一目标的最佳方法。在

我有一个xarray数据集(但它很容易是pandas数据帧或numpy数组的列表)。在

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

我想画一个散点图a和B+scale*C,其中的比例是由交互式滑块确定的。在

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

所有这一切的警告是,我希望它是在线和互动的其他人使用。在

似乎有很多选择:

  • Jupyter(我不使用笔记本,所以我对它们不太熟悉,但是 使用mybinder,我想这很容易实现)
  • 密谋地
  • Bokeh服务器
  • 在pyviz.org网站(这是非常有趣的一个,但是 在如何实现这一点上有很多选择)

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


Tags: 数据方法numpy目标pandas列表选项数组
1条回答
网友
1楼 · 发布于 2024-04-27 17:35:59

确实有很多选择,我不确定什么是最好的,但我经常使用博凯,对此我很满意。下面的例子可以帮助你开始。要启动此命令,请在保存脚本的目录中打开cmd并运行“bokeh serve脚本.py显示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=*"

allow websocket origin=*允许其他用户访问服务器并查看图表。http将是http://yourPCservername:5006/(5006是默认的bokeh端口)。如果你不想从你的电脑上服务,你可以订阅一个云服务,比如Heroku:example。在

相关问题 更多 >