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

Skip to content

Commit 396c7e2

Browse files
committed
Set image cmap from figure options.
This patch allows the user to interactively set an image's colormap from the axes and lines (and now images) parameters editor (in the Qt backend). Colormaps are listed from those registered with matplotlib.cm. Colorbars seem to be handled properly.
1 parent 4cab2e7 commit 396c7e2

File tree

3 files changed

+98
-74
lines changed

3 files changed

+98
-74
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,8 @@ def add_image(self, image):
16811681
Returns the image.
16821682
"""
16831683
self._set_artist_props(image)
1684+
if not image.get_label():
1685+
image.set_label('_image%d' % len(self.images))
16841686
self.images.append(image)
16851687
image._remove_method = lambda h: self.images.remove(h)
16861688
return image

lib/matplotlib/backends/backend_qt5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ def _init_toolbar(self):
586586
if figureoptions is not None:
587587
a = self.addAction(self._icon("qt4_editor_options.png"),
588588
'Customize', self.edit_parameters)
589-
a.setToolTip('Edit curves line and axes parameters')
589+
a.setToolTip('Edit axis, curve and image parameters')
590590

591591
self.buttons = {}
592592

lib/matplotlib/backends/qt_editor/figureoptions.py

Lines changed: 95 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import matplotlib.backends.qt_editor.formlayout as formlayout
1818
from matplotlib.backends.qt_compat import QtGui
19-
from matplotlib import markers
19+
from matplotlib import cm, markers
2020
from matplotlib.colors import colorConverter, rgb2hex
2121

2222

@@ -43,8 +43,6 @@ def figure_edit(axes, parent=None):
4343
"""Edit matplotlib figure options"""
4444
sep = (None, None) # separator
4545

46-
has_curve = len(axes.get_lines()) > 0
47-
4846
# Get / General
4947
xmin, xmax = axes.get_xlim()
5048
ymin, ymax = axes.get_ylim()
@@ -69,66 +67,86 @@ def figure_edit(axes, parent=None):
6967
xunits = axes.xaxis.get_units()
7068
yunits = axes.yaxis.get_units()
7169

72-
if has_curve:
73-
# Get / Curves
74-
linedict = {}
75-
for line in axes.get_lines():
76-
label = line.get_label()
77-
if label == '_nolegend_':
78-
continue
79-
linedict[label] = line
80-
curves = []
81-
82-
def prepare_data(d, init):
83-
"""Prepare entry for FormLayout.
84-
"""
85-
# List items in dict, dropping duplicate values, sorting by values.
86-
kvs = [(k, v) for v, k in
87-
sorted({v: k for k, v in d.items()}.items())]
88-
# Find the unique kept key with the same value as the init value.
89-
canonical_init, = ({k for k, v in d.items() if v == d[init]}.
90-
intersection(k for k, v in kvs))
91-
return [canonical_init] + kvs
92-
93-
curvelabels = sorted(linedict.keys())
94-
for label in curvelabels:
95-
line = linedict[label]
96-
color = rgb2hex(colorConverter.to_rgb(line.get_color()))
97-
ec = rgb2hex(colorConverter.to_rgb(line.get_markeredgecolor()))
98-
fc = rgb2hex(colorConverter.to_rgb(line.get_markerfacecolor()))
99-
curvedata = [
100-
('Label', label),
101-
sep,
102-
(None, '<b>Line</b>'),
103-
('Line Style', prepare_data(LINESTYLES, line.get_linestyle())),
104-
('Draw Style', prepare_data(DRAWSTYLES, line.get_drawstyle())),
105-
('Width', line.get_linewidth()),
106-
('Color', color),
107-
sep,
108-
(None, '<b>Marker</b>'),
109-
('Style', prepare_data(MARKERS, line.get_marker())),
110-
('Size', line.get_markersize()),
111-
('Facecolor', fc),
112-
('Edgecolor', ec)]
113-
curves.append([curvedata, label, ""])
114-
115-
# make sure that there is at least one displayed curve
116-
has_curve = bool(curves)
70+
def style_data(d, init):
71+
"""Prepare style entry for FormLayout.
72+
"""
73+
# List items in dict, dropping duplicate values, sorting by values.
74+
kvs = [(k, v) for v, k in sorted({v: k for k, v in d.items()}.items())]
75+
# Find the unique kept key with the same value as the init value.
76+
canonical_init, = ({k for k, v in d.items() if v == d[init]}.
77+
intersection(k for k, v in kvs))
78+
return [canonical_init] + kvs
79+
80+
# Get / Curves
81+
linedict = {}
82+
for line in axes.get_lines():
83+
label = line.get_label()
84+
if label == '_nolegend_':
85+
continue
86+
linedict[label] = line
87+
curvelabels = sorted(linedict)
88+
curves = []
89+
for label in curvelabels:
90+
line = linedict[label]
91+
color = rgb2hex(colorConverter.to_rgb(line.get_color()))
92+
ec = rgb2hex(colorConverter.to_rgb(line.get_markeredgecolor()))
93+
fc = rgb2hex(colorConverter.to_rgb(line.get_markerfacecolor()))
94+
curvedata = [
95+
('Label', label),
96+
sep,
97+
(None, '<b>Line</b>'),
98+
('Line Style', style_data(LINESTYLES, line.get_linestyle())),
99+
('Draw Style', style_data(DRAWSTYLES, line.get_drawstyle())),
100+
('Width', line.get_linewidth()),
101+
('Color', color),
102+
sep,
103+
(None, '<b>Marker</b>'),
104+
('Style', style_data(MARKERS, line.get_marker())),
105+
('Size', line.get_markersize()),
106+
('Facecolor', fc),
107+
('Edgecolor', ec)]
108+
curves.append([curvedata, label, ""])
109+
has_curve = bool(curves)
110+
111+
# Get / Images
112+
imagedict = {}
113+
for image in axes.get_images():
114+
label = image.get_label()
115+
if label == '_nolegend_':
116+
continue
117+
imagedict[label] = image
118+
imagelabels = sorted(imagedict)
119+
images = []
120+
cmaps = [(cmap, name) for name, cmap in sorted(cm.cmap_d.items())]
121+
for label in imagelabels:
122+
image = imagedict[label]
123+
cmap = image.get_cmap()
124+
if cmap not in cm.cmap_d:
125+
cmaps = [(cmap, cmap.name)] + cmaps
126+
imagedata = [
127+
('Label', label),
128+
('Colormap', [cmap.name] + cmaps)
129+
]
130+
images.append([imagedata, label, ""])
131+
has_image = bool(images)
117132

118133
datalist = [(general, "Axes", "")]
119-
if has_curve:
134+
if curves:
120135
datalist.append((curves, "Curves", ""))
136+
if images:
137+
datalist.append((images, "Images", ""))
121138

122139
def apply_callback(data):
123140
"""This function will be called to apply changes"""
124-
if has_curve:
125-
general, curves = data
126-
else:
127-
general, = data
141+
general = data.pop(0)
142+
curves = data.pop(0) if has_curve else []
143+
images = data.pop(0) if has_image else []
144+
if data:
145+
raise ValueError("Unexpected field")
128146

129147
# Set / General
130-
title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale, \
131-
generate_legend = general
148+
(title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale,
149+
generate_legend) = general
132150
axes.set_xscale(xscale)
133151
axes.set_yscale(yscale)
134152
axes.set_title(title)
@@ -145,26 +163,30 @@ def apply_callback(data):
145163
axes.xaxis._update_axisinfo()
146164
axes.yaxis._update_axisinfo()
147165

148-
if has_curve:
149-
# Set / Curves
150-
for index, curve in enumerate(curves):
151-
line = linedict[curvelabels[index]]
152-
label, linestyle, drawstyle, linewidth, color, \
153-
marker, markersize, markerfacecolor, markeredgecolor \
154-
= curve
155-
line.set_label(label)
156-
line.set_linestyle(linestyle)
157-
line.set_drawstyle(drawstyle)
158-
line.set_linewidth(linewidth)
159-
line.set_color(color)
160-
if marker is not 'none':
161-
line.set_marker(marker)
162-
line.set_markersize(markersize)
163-
line.set_markerfacecolor(markerfacecolor)
164-
line.set_markeredgecolor(markeredgecolor)
166+
# Set / Curves
167+
for index, curve in enumerate(curves):
168+
line = linedict[curvelabels[index]]
169+
(label, linestyle, drawstyle, linewidth, color, marker, markersize,
170+
markerfacecolor, markeredgecolor) = curve
171+
line.set_label(label)
172+
line.set_linestyle(linestyle)
173+
line.set_drawstyle(drawstyle)
174+
line.set_linewidth(linewidth)
175+
line.set_color(color)
176+
if marker is not 'none':
177+
line.set_marker(marker)
178+
line.set_markersize(markersize)
179+
line.set_markerfacecolor(markerfacecolor)
180+
line.set_markeredgecolor(markeredgecolor)
181+
182+
# Set / Images
183+
for index, image_settings in enumerate(images):
184+
image = imagedict[imagelabels[index]]
185+
label, cmap = image_settings
186+
image.set_label(label)
187+
image.set_cmap(cm.get_cmap(cmap))
165188

166189
# re-generate legend, if checkbox is checked
167-
168190
if generate_legend:
169191
draggable = None
170192
ncol = None

0 commit comments

Comments
 (0)