Note
Click here to download the full example code
11. Using updatemenus
This example show how to include buttons in the plotly graph by using update menus. Work should be put on control their placement and how to avoid them to be missaligned when resizing the window!
Warning
It is not finished!
15 import plotly
16 import plotly.graph_objs as go
17
18 from plotly.io import show
19
20 try:
21 __file__
22 TERMINAL = True
23 except:
24 TERMINAL = False
25
26
27 def get_color_set(color_set_id):
28 """Get color set."""
29 if color_set_id == 1:
30 marker_color = ['red', 'green', 'blue']
31 elif color_set_id == 2:
32 marker_color = ['black', 'blue', 'red']
33 return [{'marker.color': [marker_color]}];
34
35
36 # Define trace
37 trace = go.Scatter(
38 x=[0,1,1],
39 y=[1,0,1],
40 marker=dict(color=['green','black','red']),
41 mode='markers'
42 )
43
44 # Define updatemenus
45 updatemenus=list([
46 dict(
47 buttons=list([
48 dict(label = 'Color Set 1',
49 method = 'update',
50 args=get_color_set(1)
51 ),
52 dict(label = 'Color Set 2',
53 method = 'update',
54 args=get_color_set(2)
55 ),
56 ]),
57 direction = 'left',
58 pad = {'r': 10, 't': 10},
59 showactive = True,
60 type = 'buttons',
61 x = 0.1,
62 xanchor = 'left',
63 y = 1.1,
64 yanchor = 'top'
65 )
66 ])
67
68 # Define layout
69 layout = go.Layout(
70 title='Scatter Color Switcher',
71 updatemenus = updatemenus
72 )
73
74 # Create Figure
75 fig = go.Figure(data=[trace], layout=layout)
76
77 # ----------------------------
78 # Save
79 # ----------------------------
80 # Libraries
81 import time
82 from pathlib import Path
83
84 # Define pipeline path
85 path = Path('./objects') / 'plot_main11_updatemenus'
86 filename = '%s.html' % time.strftime("%Y%m%d-%H%M%S")
87
88 # Create folder (if it does not exist)
89 path.mkdir(parents=True, exist_ok=True)
90
91 # Save
92 fig.write_html("%s/%s" % (path, filename))
93
94 # Show
95 show(fig)
Total running time of the script: ( 0 minutes 0.127 seconds)