Обновление FigureWidget с помощью ipywidgets - PullRequest
0 голосов
/ 30 апреля 2019

Я хочу использовать widgets для отображения другого plotly бокса в зависимости от того, что выбрал пользователь.У меня проблема с обновлением исходных FigureWidget новыми данными FigureWidget.

См. Мой код ниже:

import plotly
import plotly.offline as py
import plotly.graph_objs as go
from plotly import tools
from ipywidgets import widgets, interact

plotly.offline.init_notebook_mode() # run at the start of every notebook

types_of_fruit = {'citrus' : {'orange', 'grapefruit', 'mandarins', 'limes'},
                 'berries' : {'strawberry', 'raspberry', 'blueberry', 'kiwi', 'passionfruit'},
                 'melons' : {'watermelon', 'honeydew'}}

fruit_stats = {'orange' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
              'grapefruit' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
              'mandarins' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
              'limes' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
              'strawberry' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
              'raspberry' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
              'blueberry' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
              'kiwi' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
              'passionfruit' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
              'watermelon' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
              'honeydew' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00]}

default_fruit_type = 'citrus'
default_data = []
default_data_batched = []
default_traces = []
new_data = []
new_data_batched = []
no_of_boxplots_per_row = 3
figure = None
fig = None


for fruit in types_of_fruit[default_fruit_type]:
    fruit_vals = fruit_stats[fruit]
    default_data.append(go.Box(y=fruit_vals , name=fruit, showlegend=True))

default_data_batched = [default_data[x:x+no_of_boxplots_per_row] 
                    for x in xrange(0, len(default_data), no_of_boxplots_per_row)]

figure = tools.make_subplots(rows=len(default_data_batched), cols=1)

for i,batches in enumerate(default_data_batched, start=1):
    for plot in batches:
        figure.append_trace(plot, i, 1)

figure['layout'].update(height=(500 * len(default_data_batched)), width=1000, title=default_fruit_type)
f = go.FigureWidget(figure)

fruit_type_widget = widgets.Dropdown(
    options=list(types_of_fruit.keys()),
    value=default_fruit_type,
    description='Fruit_type:',
)


def response(change):
    new_data = []
    no_of_boxplots_per_row = 25
    fruit_type = change['new']
    print factor_type
    for fruit in types_of_fruit[fruit_type]:
        fruit_vals = fruit_stats[fruit]
        new_data.append(go.Box(y=fruit_vals, name=fruit, showlegend=True))
    new_data_batched = [new_data[x:x+no_of_boxplots_per_row] for x in xrange(0, len(new_data), no_of_boxplots_per_row)]
    fig = tools.make_subplots(rows=len(new_data_batched), cols=1)
    fig['layout'].update(height=(500 * len(new_data_batched)), width=1000, title=fruit_type)


    for i,batches in enumerate(new_data_batched, start=1):
        for trace in batches:
            fig.append_trace(trace, i, 1)

    f2 = go.FigureWidget(fig)
    f['data'] = f2['data']
    f['layout'] = f['layout']


fruit_type_widget.observe(response, names="value")
widgets.VBox([fruit_type_widget, f])

Это ошибка, которую я получаю, когда выбираю другой fruit_type из выпадающего списка:

 ValueError: The data property of a figure may only be assigned a list or tuple that contains a permutation of a subset of itself
Invalid trace(s) with uid(s): set(['54fca825-2ba9-4e1c-b29a-09e6f859610b', '15236ab4-2238-4ed1-af3e-0ce9880ab3c7'])

Есть лиспособ эффективно сделать это?

Я пытался использовать f.update(), но это не сработало, если у каждого fruit_type не было одинакового количества коробок.

...