Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. import plotly
  2. import plotly.offline as py
  3. import plotly.graph_objs as go
  4. from plotly import tools
  5. from ipywidgets import widgets, interact
  6.  
  7. plotly.offline.init_notebook_mode() # run at the start of every notebook
  8.  
  9. types_of_fruit = {'citrus' : {'orange', 'grapefruit', 'mandarins', 'limes'},
  10. 'berries' : {'strawberry', 'raspberry', 'blueberry', 'kiwi', 'passionfruit'},
  11. 'melons' : {'watermelon', 'honeydew'}}
  12.  
  13. fruit_stats = {'orange' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
  14. 'grapefruit' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
  15. 'mandarins' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
  16. 'limes' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
  17. 'strawberry' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
  18. 'raspberry' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
  19. 'blueberry' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
  20. 'kiwi' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
  21. 'passionfruit' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
  22. 'watermelon' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00],
  23. 'honeydew' : [0.00, 25.00, 25.00, 50.00, 75.00, 75.00, 100.00]}
  24.  
  25. default_fruit_type = 'citrus'
  26. default_data = []
  27. default_data_batched = []
  28. default_traces = []
  29. new_data = []
  30. new_data_batched = []
  31. no_of_boxplots_per_row = 3
  32. figure = None
  33. fig = None
  34.  
  35.  
  36. for fruit in types_of_fruit[default_fruit_type]:
  37. fruit_vals = fruit_stats[fruit]
  38. default_data.append(go.Box(y=factor_vals, name=fruit, showlegend=True))
  39.  
  40. default_data_batched = [default_data[x:x+no_of_boxplots_per_row]
  41. for x in xrange(0, len(default_data), no_of_boxplots_per_row)]
  42.  
  43. figure = tools.make_subplots(rows=len(default_data_batched), cols=1)
  44.  
  45. for i,batches in enumerate(default_data_batched, start=1):
  46. for plot in batches:
  47. figure.append_trace(plot, i, 1)
  48.  
  49. figure['layout'].update(height=(500 * len(default_data_batched)), width=1000, title=default_fruit_type)
  50. f = go.FigureWidget(figure)
  51.  
  52. fruit_type_widget = widgets.Dropdown(
  53. options=list(types_of_fruit.keys()),
  54. value=default_fruit_type,
  55. description='Fruit_type:',
  56. )
  57.  
  58.  
  59. def response(change):
  60. new_data = []
  61. no_of_boxplots_per_row = 25
  62. fruit_type = change['new']
  63. print factor_type
  64. for fruit in types_of_fruit[fruit_type]:
  65. fruit_vals = fruit_stats[fruit]
  66. new_data.append(go.Box(y=factor_vals, name=fruit, showlegend=True))
  67. 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)]
  68. fig = tools.make_subplots(rows=len(new_data_batched), cols=1)
  69. fig['layout'].update(height=(500 * len(new_data_batched)), width=1000, title=fruit_type)
  70.  
  71.  
  72. for i,batches in enumerate(new_data_batched, start=1):
  73. for trace in batches:
  74. fig.append_trace(trace, i, 1)
  75.  
  76. f2 = go.FigureWidget(fig)
  77. f['data'] = f2['data']
  78. f['layout'] = f['layout']
  79.  
  80.  
  81. fruit_type_widget.observe(response, names="value")
  82. widgets.VBox([fruit_type_widget, f])
  83.  
  84. ValueError: The data property of a figure may only be assigned a list or tuple that contains a permutation of a subset of itself
  85. Invalid trace(s) with uid(s): set(['54fca825-2ba9-4e1c-b29a-09e6f859610b', '15236ab4-2238-4ed1-af3e-0ce9880ab3c7'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement