为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

UYOU 2021-11-09 19:31:38
我希望有人能指出我正确的方向。python datavis 领域现在已经变得巨大,并且有很多选择,我有点迷失了实现这一目标的最佳方法是什么。我有一个 xarray 数据集(但它很容易是一个 Pandas 数据框或一个 numpy 数组列表)。我有 3 列,A、B 和 C。它们包含 40 个数据点。我想绘制 A vs B + scale*C 的散点图,其中比例是从交互式滑块确定的。更高级的版本会有一个下拉列表,您可以在其中选择一组不同的 3 列,但稍后我会担心这一点。对所有这些的警告是,我希望它在线并且可以交互以供其他人使用。似乎有很多选择:Jupyter(我不使用笔记本,所以我对它们不太熟悉,但使用 mybinder 我认为这很容易做到)情节散景服务器pyviz.org(这是非常有趣的一个,但同样,关于如何实现这一点似乎有很多选择)任何想法或建议将不胜感激。
查看完整描述

1 回答

?
慕容3067478

TA贡献1773条经验 获得超3个赞

确实有很多选择,我不确定什么是最好的,但我经常使用散景并对此感到高兴。下面的示例可以帮助您入门。要启动它,请在保存脚本的目录中打开一个 cmd 并运行“bokeh serve 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=*"

allow-websocket-origin=* 是允许其他用户访问服务器并查看图表。http 将是http://yourPCservername:5006/(5006 是默认的散景端口)。如果您不想从您的 PC 上提供服务,您可以订阅像 Heroku 这样的云服务:example。


查看完整回答
反对 回复 2021-11-09
  • 1 回答
  • 0 关注
  • 129 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信