绘图-使用共享的滑块变量进行多条迹线 [英] plotly - multiple traces using a shared slider variable
问题描述
正如标题所示,我正在努力创建一个绘图图表,其中包含多条线,这些线是同一滑块变量的函数.
As the title hints, I'm struggling to create a plotly chart that has multiple lines that are functions of the same slider variable.
我使用文档中的零碎内容一起破解了一些东西: https://pastebin.com/eBixANqA .这适用于一行.
I hacked something together using bits and pieces from the documentation: https://pastebin.com/eBixANqA. This works for one line.
现在,我想在同一张图表中添加更多折线,但这就是我正在努力的地方. https://pastebin.com/qZCMGeAa .
我得到一个PlotlyListEntryError: Invalid entry found in 'data' at index, '0'
Path To Error: ['data'][0]
Now I want to add more lines to the same chart, but this is where I'm struggling. https://pastebin.com/qZCMGeAa.
I'm getting a PlotlyListEntryError: Invalid entry found in 'data' at index, '0'
Path To Error: ['data'][0]
有人可以帮忙吗?
推荐答案
您似乎正在使用 https: //plot.ly/python/sliders/作为参考,很遗憾,我没有时间测试您的代码,但这应该很容易适应.如果创建每条迹线,都希望以与以前相同的方式进行绘制:
It looks like you were using https://plot.ly/python/sliders/ as a reference, unfortunately I don't have time to test with your code, but this should be easily adaptable. If you create each trace you want to plot in the same way that you have been:
trace1 = [dict(
type='scatter',
visible = False,
name = "trace title",
mode = 'markers+lines',
x = x[0:step],
y = y[0:step]) for step in range(len(x))]
在示例中我注意到,我的数据来自预定义列表,您正在使用函数,这可能是您真正需要做的唯一更改,除了您自己的步长等.
where I note in my example my data is coming from pre-defined lists, where you are using a function, that's probably the only change you'll really need to make besides your own step size etc.
如果您以相同的方式创建第二条跟踪,例如
If you create a second trace in the same way, for example
trace2 = [dict(
type='scatter',
visible = False,
name = "trace title",
mode = 'markers+lines',
x = x2[0:step],
y = y2[0:step]) for step in range(len(x2))]`
然后,您可以将所有数据与以下内容放在一起
Then you can put all your data together with the following
all_traces = trace1 + trace2
然后您可以继续进行绘制,前提是您已正确设置了布局(在单个跟踪示例中应保持不变):
then you can just go ahead and plot it provided you have your layout set up correctly (it should remain unchanged from your single trace example):
fig = py.graph_objs.Figure(data=all_traces, layout=layout)
py.offline.iplot(fig)
如果您遵循 https://plot.ly/python/sliders/,则您的滑块应控制两条轨迹使滑块正常工作.您可以通过这种方式组合多个数据字典,以使多个图由同一滑块控制.
Your slider should control both traces provided you were following https://plot.ly/python/sliders/ to get the slider working. You can combine multiple data dictionaries this way in order to have multiple plots controlled by the same slider.
我确实注意到,如果您的包含数据的字典列表的长度不同,那么这将使您感到混乱.
I do note that if your lists of dictionaries containing data are of different length, that this gets topsy-turvy.
这篇关于绘图-使用共享的滑块变量进行多条迹线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!