55import numpy as np
66import pandas as pd
77import polars as pl
8- from matplotlib .lines import Line2D
98
9+ from ert .gui .plotting .shared_plots .generic_boxplot_with_scatter import (
10+ generate_legend_items ,
11+ generate_plots ,
12+ )
1013from ert .gui .plotting .utils import PlotTools
1114from ert .gui .plotting .utils .plot_context import PlotType
1215
@@ -178,15 +181,9 @@ def _plot_misfits(
178181
179182 all_unique_indexes = all_misfits ["key_index" ].unique ().sort ().to_list ()
180183 plot_context .plot_type = PlotType .BOX
181- config = plot_context .plotConfig ()
182- outlier = plot_context .outliers
183- scatter = plot_context .scatter_plot
184- box = plot_context .box_plot
185- mean = plot_context .mean
186184
187185 index_to_pos = {idx : i for i , idx in enumerate (all_unique_indexes )}
188186
189- many_boxes_factor = min (1 , len (all_unique_indexes ) / 50 )
190187 sorted_ensemble_keys = sorted (data_with_misfits .keys ())
191188 color_map = self ._map_ensembles_to_colours (
192189 sorted_ensemble_keys , plot_context .plotConfig ().line_color_cycle ()
@@ -208,6 +205,8 @@ def _plot_misfits(
208205
209206 for ens_idx , ens_key in enumerate (sorted_ensemble_keys ):
210207 color = color_map .get (ens_key )
208+ if color is None :
209+ continue
211210
212211 df = data_with_misfits [ens_key ]
213212 if df .is_empty ():
@@ -227,138 +226,23 @@ def _plot_misfits(
227226 s .to_numpy () if s .len () > 0 else np .array ([np .nan ])
228227 for s in grouped_misfits ["misfit" ]
229228 ]
230- if box :
231- axes .boxplot (
232- data_for_boxes ,
233- positions = positions ,
234- widths = box_width ,
235- whis = (LOWER_PERCENTILE_FOR_WHISKERS , UPPER_PERCENTILE_FOR_WHISKERS ),
236- showfliers = outlier ,
237- manage_ticks = False ,
238- patch_artist = True ,
239- boxprops = {
240- "facecolor" : color ,
241- "alpha" : 0.8 ,
242- "edgecolor" : color ,
243- "linewidth" : 0.7 ,
244- },
245- whiskerprops = {
246- "color" : color ,
247- "alpha" : 1 ,
248- "linewidth" : 0.8 ,
249- "linestyle" : "--" ,
250- },
251- capprops = {"color" : color , "alpha" : 1 , "linewidth" : 0.8 },
252- medianprops = {"color" : "black" , "linewidth" : 0.8 , "alpha" : 1 },
253- flierprops = {
254- "marker" : "o" ,
255- "alpha" : 1 ,
256- "markeredgewidth" : 0.3 + (0.4 * (1 - many_boxes_factor )),
257- "markeredgecolor" : color ,
258- "markerfacecolor" : "none" ,
259- },
260- )
261-
262- if mean :
263- means = np .array (
264- [np .nanmean (arr ) for arr in data_for_boxes ], dtype = float
265- )
266- axes .plot (
267- positions ,
268- means ,
269- "D" ,
270- markersize = 4 ,
271- color = "black" ,
272- zorder = 3 , # Above boxes and scatter
273- )
274-
275- if scatter :
276- rng = np .random .default_rng (42 )
277- jitter = box_width * 0.5
278-
279- x_points : list [np .ndarray ] = []
280- y_points : list [np .ndarray ] = []
281- for position , box_data in zip (positions , data_for_boxes , strict = True ):
282- x_points .append (
283- position
284- + rng .uniform (- jitter / 2 , jitter / 2 , size = len (box_data ))
285- )
286- y_points .append (box_data )
287-
288- x_all = np .concatenate (x_points )
289- y_all = np .concatenate (y_points )
290-
291- axes .scatter (
292- x_all ,
293- y_all ,
294- color = color ,
295- alpha = 0.35 ,
296- linewidths = 0 ,
297- zorder = 2 , # above bands/boxes
298- )
299-
300- config .add_legend_item (
301- ens_key [0 ],
302- Line2D (
303- [],
304- [],
305- marker = "s" ,
306- linestyle = "None" ,
307- color = color ,
308- label = ens_key [0 ],
309- ),
310- )
311-
312- if scatter :
313- config .add_legend_item (
314- "Scatter points" ,
315- Line2D (
316- [0 ],
317- [0 ],
318- marker = "o" ,
319- color = "black" ,
320- markeredgecolor = "None" ,
321- linestyle = "None" ,
322- alpha = 0.35 ,
323- ),
324- )
325-
326- if box :
327- config .add_legend_item (
328- "Median" , Line2D ([0 ], [0 ], color = "black" , linewidth = 0.6 , alpha = 1 )
329- )
330- config .add_legend_item (
331- "Whiskers (5-95%)" ,
332- Line2D ([0 ], [0 ], color = "black" , linewidth = 0.7 , linestyle = "--" , alpha = 1 ),
229+ generate_plots (
230+ plot_context ,
231+ axes ,
232+ data_for_boxes ,
233+ positions ,
234+ box_width ,
235+ color ,
236+ LOWER_PERCENTILE_FOR_WHISKERS ,
237+ UPPER_PERCENTILE_FOR_WHISKERS ,
238+ legend_label = ens_key [0 ],
333239 )
334240
335- if mean :
336- config .add_legend_item (
337- "Mean" ,
338- Line2D (
339- [0 ],
340- [0 ],
341- marker = "D" ,
342- color = "black" ,
343- markersize = 4 ,
344- linestyle = "None" ,
345- alpha = 1 ,
346- ),
347- )
348- if outlier and box :
349- config .add_legend_item (
350- "Outliers" ,
351- Line2D (
352- [0 ],
353- [0 ],
354- marker = "o" ,
355- color = "none" ,
356- markeredgecolor = "black" ,
357- markerfacecolor = "none" ,
358- markersize = 6 ,
359- alpha = 1 ,
360- ),
361- )
241+ generate_legend_items (
242+ plot_context ,
243+ LOWER_PERCENTILE_FOR_WHISKERS ,
244+ UPPER_PERCENTILE_FOR_WHISKERS ,
245+ )
362246
363247 axes .set_xlim (- 0.5 , len (all_unique_indexes ) - 0.5 )
364248 if summary_or_breakthrough :
0 commit comments