Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 731ad32

Browse files
committed
Include scatter plots in Qt figure options editor.
Essentially all the image-handling code can be reused; the only difference is that collections don't have an `interpolation` field so we need to check for that. The rest of the PR is just replacing "image" by "sm" ("ScalarMappable") throughout...
1 parent 4dbbd22 commit 731ad32

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

lib/matplotlib/backends/qt_editor/figureoptions.py

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -137,39 +137,43 @@ def prepare_data(d, init):
137137
# Is there a curve displayed?
138138
has_curve = bool(curves)
139139

140-
# Get / Images
141-
imagedict = {}
142-
for image in axes.get_images():
143-
label = image.get_label()
140+
# Get ScalarMappables.
141+
smdict = {}
142+
for sm in [*axes.images, *axes.collections]:
143+
label = sm.get_label()
144144
if label == '_nolegend_':
145145
continue
146-
imagedict[label] = image
147-
imagelabels = sorted(imagedict, key=cmp_key)
148-
images = []
146+
smdict[label] = sm
147+
smlabels = sorted(smdict, key=cmp_key)
148+
sms = []
149149
cmaps = [(cmap, name) for name, cmap in sorted(cm.cmap_d.items())]
150-
for label in imagelabels:
151-
image = imagedict[label]
152-
cmap = image.get_cmap()
150+
for label in smlabels:
151+
sm = smdict[label]
152+
cmap = sm.get_cmap()
153153
if cmap not in cm.cmap_d.values():
154-
cmaps = [(cmap, cmap.name)] + cmaps
155-
low, high = image.get_clim()
156-
imagedata = [
154+
cmaps = [(cmap, cmap.name), *cmaps]
155+
low, high = sm.get_clim()
156+
smdata = [
157157
('Label', label),
158158
('Colormap', [cmap.name] + cmaps),
159159
('Min. value', low),
160160
('Max. value', high),
161-
('Interpolation',
162-
[image.get_interpolation()]
163-
+ [(name, name) for name in sorted(mimage.interpolations_names)])]
164-
images.append([imagedata, label, ""])
165-
# Is there an image displayed?
166-
has_image = bool(images)
161+
]
162+
if hasattr(sm, "get_interpolation"): # Images.
163+
smdata.append((
164+
'Interpolation',
165+
[image.get_interpolation(),
166+
*((name, name)
167+
for name in sorted(mimage.interpolations_names))]))
168+
sms.append([smdata, label, ""])
169+
# Is there a scalarmappable displayed?
170+
has_sm = bool(sms)
167171

168172
datalist = [(general, "Axes", "")]
169173
if curves:
170174
datalist.append((curves, "Curves", ""))
171-
if images:
172-
datalist.append((images, "Images", ""))
175+
if sms:
176+
datalist.append((sms, "Images, etc.", ""))
173177

174178
def apply_callback(data):
175179
"""This function will be called to apply changes"""
@@ -178,7 +182,7 @@ def apply_callback(data):
178182

179183
general = data.pop(0)
180184
curves = data.pop(0) if has_curve else []
181-
images = data.pop(0) if has_image else []
185+
sms = data.pop(0) if has_sm else []
182186
if data:
183187
raise ValueError("Unexpected field")
184188

@@ -223,14 +227,20 @@ def apply_callback(data):
223227
line.set_markerfacecolor(markerfacecolor)
224228
line.set_markeredgecolor(markeredgecolor)
225229

226-
# Set / Images
227-
for index, image_settings in enumerate(images):
228-
image = imagedict[imagelabels[index]]
229-
label, cmap, low, high, interpolation = image_settings
230-
image.set_label(label)
231-
image.set_cmap(cm.get_cmap(cmap))
232-
image.set_clim(*sorted([low, high]))
233-
image.set_interpolation(interpolation)
230+
# Set ScalarMappables.
231+
for index, sm_settings in enumerate(sms):
232+
sm = smdict[smlabels[index]]
233+
sentinel = object()
234+
if len(sm_settings) == 5:
235+
label, cmap, low, high, interpolation = sm_settings
236+
elif len(sm_settings) == 4:
237+
label, cmap, low, high = sm_settings
238+
interpolation = sentinel
239+
sm.set_label(label)
240+
sm.set_cmap(cm.get_cmap(cmap))
241+
sm.set_clim(*sorted([low, high]))
242+
if interpolation is not sentinel:
243+
sm.set_interpolation(interpolation)
234244

235245
# re-generate legend, if checkbox is checked
236246
if generate_legend:

0 commit comments

Comments
 (0)