1818from matplotlib import cm
1919from matplotlib .colors import Normalize
2020
21-
2221from solarforecastarbiter import datamodel
2322from solarforecastarbiter .metrics .event import _event2count
2423import solarforecastarbiter .plotting .utils as plot_utils
@@ -62,6 +61,44 @@ def gen_grays(num_colors):
6261 'font' : {'size' : 14 }
6362}
6463
64+ SORT_UPDATEMENU_DROPDOWN = [{
65+ "buttons" : [
66+ dict (
67+ method = "restyle" ,
68+ label = "Original Order" ,
69+ args = [{'visible' : [True , False , False , False , False ]}],
70+ ),
71+ dict (
72+ method = "restyle" ,
73+ label = "ᐁ Value" ,
74+ args = [{'visible' : [False , True , False , False , False ]}],
75+ ),
76+ dict (
77+ method = "restyle" ,
78+ label = "ᐃ Value" ,
79+ args = [{'visible' : [False , False , True , False , False ]}],
80+ ),
81+ dict (
82+ method = "restyle" ,
83+ label = "ᐁ Name" ,
84+ args = [{'visible' : [False , False , False , True , False ]}],
85+ ),
86+ dict (
87+ method = "restyle" ,
88+ label = "ᐃ Name" ,
89+ args = [{'visible' : [False , False , False , False , True ]}],
90+ )
91+ ],
92+ "direction" : "down" ,
93+ "showactive" : True ,
94+ "xanchor" : 'center' ,
95+ "x" : 0.025 ,
96+ "yanchor" : 'bottom' ,
97+ "pad" : {'b' : 5 },
98+ "active" : 0 ,
99+ }
100+ ]
101+
65102# Used to adjust plot height when many x axis labels or long labels are
66103# present. The length of the longest label of the plot will be multiplies by
67104# this value and added o the height of PLOT_LAYOUT_DEFAULTS to determine the
@@ -900,6 +937,7 @@ def bar(df, metric):
900937 x_values = pd .Series (x_values , name = 'abbrev' )
901938 palette = cycle (PALETTE )
902939 palette = [next (palette ) for _ in x_values ]
940+ data = data .assign (palette = palette )
903941 metric_name = datamodel .ALLOWED_METRICS [metric ]
904942
905943 # remove height limit when long abbreviations are used or there are more
@@ -921,14 +959,45 @@ def bar(df, metric):
921959 elif longest_x_label > 30 :
922960 x_axis_kwargs .update ({'tickangle' : 45 })
923961
962+ # Create dataframes for each sort (name, value)
963+ data_val_asc = data .sort_values (by = ['value' , 'name' ], ascending = True )
964+ data_val_desc = data .sort_values (by = ['value' , 'name' ], ascending = False )
965+ data_name_asc = data .sort_values (by = ['name' ], ascending = True )
966+ data_name_desc = data .sort_values (by = ['name' ], ascending = False )
967+
924968 fig = go .Figure ()
925969 fig .add_trace (go .Bar (x = x_values , y = data ['value' ],
926970 text = data ['name' ],
971+ visible = True ,
927972 marker = go .bar .Marker (color = palette ),
928973 hovertemplate = '(%{text}, %{y})<extra></extra>' ))
974+ fig .add_trace (go .Bar (x = data_val_asc ['name' ], y = data_val_asc ['value' ],
975+ text = data_val_asc ['abbrev' ],
976+ visible = False ,
977+ marker = go .bar .Marker (color = data_val_asc ['palette' ]),
978+ hovertemplate = '(%{text}, %{y})<extra></extra>' ))
979+ fig .add_trace (go .Bar (x = data_val_desc ['name' ], y = data_val_desc ['value' ],
980+ text = data_val_desc ['abbrev' ],
981+ visible = False ,
982+ marker = go .bar .Marker (color = data_val_desc ['palette' ]),
983+ hovertemplate = '(%{text}, %{y})<extra></extra>' ))
984+ fig .add_trace (go .Bar (x = data_name_asc ['name' ], y = data_name_asc ['value' ],
985+ text = data_name_asc ['abbrev' ],
986+ visible = False ,
987+ marker = go .bar .Marker (color = data_name_asc ['palette' ]),
988+ hovertemplate = '(%{text}, %{y})<extra></extra>' ))
989+ fig .add_trace (go .Bar (x = data_name_desc ['name' ], y = data_name_desc ['value' ],
990+ text = data_name_desc ['abbrev' ],
991+ visible = False ,
992+ marker = go .bar .Marker (color = data_name_desc ['palette' ]),
993+ hovertemplate = '(%{text}, %{y})<extra></extra>' ))
994+ updatemenus = SORT_UPDATEMENU_DROPDOWN
995+ if len (x_values ) <= 1 :
996+ updatemenus = None
929997 fig .update_layout (
930998 title = f'<b>{ metric_name } </b>' ,
931999 xaxis_title = metric_name ,
1000+ updatemenus = updatemenus ,
9321001 ** plot_layout_args )
9331002 configure_axes (fig , x_axis_kwargs , y_range )
9341003 return fig
0 commit comments