16
16
17
17
import matplotlib .backends .qt_editor .formlayout as formlayout
18
18
from matplotlib .backends .qt_compat import QtGui
19
- from matplotlib import markers
19
+ from matplotlib import cm , markers
20
20
from matplotlib .colors import colorConverter , rgb2hex
21
21
22
22
@@ -43,8 +43,6 @@ def figure_edit(axes, parent=None):
43
43
"""Edit matplotlib figure options"""
44
44
sep = (None , None ) # separator
45
45
46
- has_curve = len (axes .get_lines ()) > 0
47
-
48
46
# Get / General
49
47
xmin , xmax = axes .get_xlim ()
50
48
ymin , ymax = axes .get_ylim ()
@@ -69,66 +67,86 @@ def figure_edit(axes, parent=None):
69
67
xunits = axes .xaxis .get_units ()
70
68
yunits = axes .yaxis .get_units ()
71
69
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 )
117
132
118
133
datalist = [(general , "Axes" , "" )]
119
- if has_curve :
134
+ if curves :
120
135
datalist .append ((curves , "Curves" , "" ))
136
+ if images :
137
+ datalist .append ((images , "Images" , "" ))
121
138
122
139
def apply_callback (data ):
123
140
"""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" )
128
146
129
147
# 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
132
150
axes .set_xscale (xscale )
133
151
axes .set_yscale (yscale )
134
152
axes .set_title (title )
@@ -145,26 +163,30 @@ def apply_callback(data):
145
163
axes .xaxis ._update_axisinfo ()
146
164
axes .yaxis ._update_axisinfo ()
147
165
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 ))
165
188
166
189
# re-generate legend, if checkbox is checked
167
-
168
190
if generate_legend :
169
191
draggable = None
170
192
ncol = None
0 commit comments