@@ -59,19 +59,23 @@ def figure_edit(axes, parent=None):
5959
6060 # Sorting for default labels (_lineXXX, _imageXXX).
6161 def cmp_key (label ):
62- match = re .match (r"(_line|_image)(\d+)" , label )
62+ """
63+ Label should be a tuple consisting of the string label,
64+ and the object being sorted by label.
65+ """
66+ match = re .match (r"(_line|_image)(\d+)" , label [0 ])
6367 if match :
6468 return match .group (1 ), int (match .group (2 ))
6569 else :
66- return label , 0
70+ return label [ 0 ] , 0
6771
6872 # Get / Curves
69- linedict = {}
73+ labeled_lines = []
7074 for line in axes .get_lines ():
7175 label = line .get_label ()
7276 if label == '_nolegend_' :
7377 continue
74- linedict [ label ] = line
78+ labeled_lines . append (( label , line ))
7579 curves = []
7680
7781 def prepare_data (d , init ):
@@ -101,9 +105,7 @@ def prepare_data(d, init):
101105 sorted (short2name .items (),
102106 key = lambda short_and_name : short_and_name [1 ]))
103107
104- curvelabels = sorted (linedict , key = cmp_key )
105- for label in curvelabels :
106- line = linedict [label ]
108+ for label , line in sorted (labeled_lines , key = cmp_key ):
107109 color = mcolors .to_hex (
108110 mcolors .to_rgba (line .get_color (), line .get_alpha ()),
109111 keep_alpha = True )
@@ -132,17 +134,15 @@ def prepare_data(d, init):
132134 has_curve = bool (curves )
133135
134136 # Get ScalarMappables.
135- mappabledict = {}
137+ labeled_mappables = []
136138 for mappable in [* axes .images , * axes .collections ]:
137139 label = mappable .get_label ()
138140 if label == '_nolegend_' or mappable .get_array () is None :
139141 continue
140- mappabledict [label ] = mappable
141- mappablelabels = sorted (mappabledict , key = cmp_key )
142+ labeled_mappables .append ((label , mappable ))
142143 mappables = []
143144 cmaps = [(cmap , name ) for name , cmap in sorted (cm ._cmap_registry .items ())]
144- for label in mappablelabels :
145- mappable = mappabledict [label ]
145+ for label , mappable in sorted (labeled_mappables , key = cmp_key ):
146146 cmap = mappable .get_cmap ()
147147 if cmap not in cm ._cmap_registry .values ():
148148 cmaps = [(cmap , cmap .name ), * cmaps ]
@@ -205,7 +205,7 @@ def apply_callback(data):
205205
206206 # Set / Curves
207207 for index , curve in enumerate (curves ):
208- line = linedict [ curvelabels [ index ]]
208+ line = labeled_lines [ index ][ 1 ]
209209 (label , linestyle , drawstyle , linewidth , color , marker , markersize ,
210210 markerfacecolor , markeredgecolor ) = curve
211211 line .set_label (label )
@@ -223,7 +223,7 @@ def apply_callback(data):
223223
224224 # Set ScalarMappables.
225225 for index , mappable_settings in enumerate (mappables ):
226- mappable = mappabledict [ mappablelabels [ index ]]
226+ mappable = labeled_mappables [ index ][ 1 ]
227227 if len (mappable_settings ) == 5 :
228228 label , cmap , low , high , interpolation = mappable_settings
229229 mappable .set_interpolation (interpolation )
0 commit comments