From 4cab2e797f5cdaf2f6ff476de67045a1a6a2a2c2 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 28 Oct 2015 16:05:38 -0700 Subject: [PATCH 1/2] Sort and uniquify style entries in figure options. Fixes the first two points of #5341. --- .../backends/qt_editor/figureoptions.py | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/backends/qt_editor/figureoptions.py b/lib/matplotlib/backends/qt_editor/figureoptions.py index 7435a47a0111..e1208ad35bbb 100644 --- a/lib/matplotlib/backends/qt_editor/figureoptions.py +++ b/lib/matplotlib/backends/qt_editor/figureoptions.py @@ -78,29 +78,38 @@ def figure_edit(axes, parent=None): continue linedict[label] = line curves = [] - linestyles = list(six.iteritems(LINESTYLES)) - drawstyles = list(six.iteritems(DRAWSTYLES)) - markers = list(six.iteritems(MARKERS)) + + def prepare_data(d, init): + """Prepare entry for FormLayout. + """ + # List items in dict, dropping duplicate values, sorting by values. + kvs = [(k, v) for v, k in + sorted({v: k for k, v in d.items()}.items())] + # Find the unique kept key with the same value as the init value. + canonical_init, = ({k for k, v in d.items() if v == d[init]}. + intersection(k for k, v in kvs)) + return [canonical_init] + kvs + curvelabels = sorted(linedict.keys()) for label in curvelabels: line = linedict[label] color = rgb2hex(colorConverter.to_rgb(line.get_color())) ec = rgb2hex(colorConverter.to_rgb(line.get_markeredgecolor())) fc = rgb2hex(colorConverter.to_rgb(line.get_markerfacecolor())) - curvedata = [('Label', label), - sep, - (None, 'Line'), - ('Line Style', [line.get_linestyle()] + linestyles), - ('Draw Style', [line.get_drawstyle()] + drawstyles), - ('Width', line.get_linewidth()), - ('Color', color), - sep, - (None, 'Marker'), - ('Style', [line.get_marker()] + markers), - ('Size', line.get_markersize()), - ('Facecolor', fc), - ('Edgecolor', ec), - ] + curvedata = [ + ('Label', label), + sep, + (None, 'Line'), + ('Line Style', prepare_data(LINESTYLES, line.get_linestyle())), + ('Draw Style', prepare_data(DRAWSTYLES, line.get_drawstyle())), + ('Width', line.get_linewidth()), + ('Color', color), + sep, + (None, 'Marker'), + ('Style', prepare_data(MARKERS, line.get_marker())), + ('Size', line.get_markersize()), + ('Facecolor', fc), + ('Edgecolor', ec)] curves.append([curvedata, label, ""]) # make sure that there is at least one displayed curve From 2bbffe17ec42eb8fb16c3912b8d7318c5ef3254e Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 12 Nov 2015 09:35:47 -0800 Subject: [PATCH 2/2] Clarify the implementation of prepare_data. --- .../backends/qt_editor/figureoptions.py | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/backends/qt_editor/figureoptions.py b/lib/matplotlib/backends/qt_editor/figureoptions.py index e1208ad35bbb..4882d8b8dd6a 100644 --- a/lib/matplotlib/backends/qt_editor/figureoptions.py +++ b/lib/matplotlib/backends/qt_editor/figureoptions.py @@ -81,14 +81,27 @@ def figure_edit(axes, parent=None): def prepare_data(d, init): """Prepare entry for FormLayout. + + `d` is a mapping of shorthands to style names (a single style may + have multiple shorthands, in particular the shorthands `None`, + `"None"`, `"none"` and `""` are synonyms); `init` is one shorthand + of the initial style. + + This function returns an list suitable for initializing a + FormLayout combobox, namely `[initial_name, (shorthand, + style_name), (shorthand, style_name), ...]`. """ - # List items in dict, dropping duplicate values, sorting by values. - kvs = [(k, v) for v, k in - sorted({v: k for k, v in d.items()}.items())] - # Find the unique kept key with the same value as the init value. - canonical_init, = ({k for k, v in d.items() if v == d[init]}. - intersection(k for k, v in kvs)) - return [canonical_init] + kvs + # Drop duplicate shorthands from dict (by overwriting them during + # the dict comprehension). + name2short = {name: short for short, name in d.items()} + # Convert back to {shorthand: name}. + short2name = {short: name for name, short in name2short.items()} + # Find the kept shorthand for the style specified by init. + canonical_init = name2short[d[init]] + # Sort by representation and prepend the initial value. + return ([canonical_init] + + sorted(short2name.items(), + key=lambda short_and_name: short_and_name[1])) curvelabels = sorted(linedict.keys()) for label in curvelabels: