plotly - multiple traces using a shared slider variable

元气小坏坏 提交于 2019-12-13 20:07:07

问题


As the title hints, I'm struggling to create a plotly chart that has multiple lines that are functions of the same slider variable.

I hacked something together using bits and pieces from the documentation: https://pastebin.com/eBixANqA. This works for one line.

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]

Can someone please help?


回答1:


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)

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.



来源:https://stackoverflow.com/questions/49057127/plotly-multiple-traces-using-a-shared-slider-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!