简体   繁体   中英

Python Plotly: use buttons to update sets of subplots & arbitrarily assign traces

Let's say I have 6 sets of data:

X = [i for i in range(100)]
Y1 = [2*i for i in range(100)]
Y2 = [3*i for i in range(100)]
Y3 = [4*i for i in range(100)]
Y4 = [5*i for i in range(100)]
Y5 = [6*i for i in range(100)]
Y6 = [7*i for i in range(100)]

Using the example here ( https://plot.ly/python/custom-buttons/ ), I can create buttons that will allow me to switch between displaying these six sets of data on a single plot. I, however, would like to separate the data onto sets of subplots, and have buttons to switch between subplot sets.

For example, I would like two sets of subplots consisting of two subplots each (so, 4 plots total, 2 sets of 2 subplots). On the first set of subplots, I would like to place Y1 & Y2 on the first plot and Y3 on the second plot. On the second set of subplots, I would like to place Y4 on the first subplot and Y5 & Y6 on the second subplot. I would then like two buttons that would allow me to switch between the first set of subplots (Y1/Y2 & Y3) and the second set of subplots (Y4, Y5/Y6).

Another example would be to have two sets of subplots consisting of 2 and 3 subplots, respectively (as opposed to 2 subplots each set). In the first set of subplots (2 subplots) placing Y1 on the first and Y2 on the second. In the second set of subplots (3 subplots) placing Y3 & Y4 on the first subplot, Y5 on the second, and Y6 on the third. The two buttons would update between the first and second set of subplots.

The ultimate goal is to have a basis for having an arbitrary number of subplot sets with an arbitrary number of subplots assigned to each subset (not the same), then arbitrarily assigning any number of traces to each subplot in each set of subplots, and use buttons to switch between the subplot sets.

If not with buttons, could this be accomplished with a menu? Also, is there a menu widget that allows one to use the up/down buttons to step through the subplot sets?

Another similar reference (but not one that addresses this specific question) can be found here: Python Plotly - Multiple dropdown plots, each of which have subplots

Below is a code I have been working on to try to develop this (it is incomplete, of course, but might serve as a basis for a solution.)

Thanks so much for your replies!

import pandas as pd
import numpy as np
import plotly.offline as offline
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot, plot
from plotly import tools
init_notebook_mode(connected=True)

# 6 sets of data
X = [i for i in range(100)]
Y1 = [2*i for i in range(100)]
Y2 = [3*i for i in range(100)]
Y3 = [4*i for i in range(100)]
Y4 = [5*i for i in range(100)]
Y5 = [6*i for i in range(100)]
Y6 = [7*i for i in range(100)]
labels = ["One", "Two"]

# 2 subplots
fig = tools.make_subplots(rows=1, cols=2)

trace1 = go.Scatter(
                x=X,
                y=Y1, 
                showlegend=False
            )
trace2 = go.Scatter(
                x=X,
                y=Y2, 
                showlegend=False
            )
trace3 = go.Scatter(
                x=X,
                y=Y3, 
                showlegend=False
            )
trace4 = go.Scatter(
                x=X,
                y=Y4, 
                showlegend=False
            )
trace5 = go.Scatter(
                x=X,
                y=Y5, 
                showlegend=False
            )
trace6 = go.Scatter(
                x=X,
                y=Y6, 
                showlegend=False
            )

data = [trace1,trace2]

updatemenus = list([
    dict(type="buttons",
         active=-1,
         buttons = list([
             dict(label = 'first',
                  method = 'update',
                  args = [{'visible':[True,False]},
                           {'title': 'first'}]),
             dict(label = 'second',
                  method = 'update',
                  args = [{'visible':[False,True]},
                           {'title': 'second'}])
        ])
    )])

layout = dict(title = 'test', showlegend=False,
              updatemenus=updatemenus)

fig = dict(data=data,layout=layout)

offline.plot(fig, filename='tmp.html')

Please check the documentation ( this link )

In the documentation the example they use to append is by using fig.append_trace() with the first argument the trace you already generated, and the second and third are the position of the subplot.

fig = tools.make_subplots(rows=1, cols=2)

fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM