|
5 | 5 |
|
6 | 6 | """Module that provides a GUI-based editor for Matplotlib's figure options."""
|
7 | 7 |
|
| 8 | +from itertools import chain |
8 | 9 | from matplotlib import cbook, cm, colors as mcolors, markers, image as mimage
|
9 | 10 | from matplotlib.backends.qt_compat import QtGui
|
10 | 11 | from matplotlib.backends.qt_editor import _formlayout
|
@@ -38,30 +39,40 @@ def convert_limits(lim, converter):
|
38 | 39 | # Cast to builtin floats as they have nicer reprs.
|
39 | 40 | return map(float, lim)
|
40 | 41 |
|
41 |
| - xconverter = axes.xaxis.converter |
42 |
| - xmin, xmax = convert_limits(axes.get_xlim(), xconverter) |
43 |
| - yconverter = axes.yaxis.converter |
44 |
| - ymin, ymax = convert_limits(axes.get_ylim(), yconverter) |
45 |
| - general = [('Title', axes.get_title()), |
46 |
| - sep, |
47 |
| - (None, "<b>X-Axis</b>"), |
48 |
| - ('Left', xmin), ('Right', xmax), |
49 |
| - ('Label', axes.get_xlabel()), |
50 |
| - ('Scale', [axes.get_xscale(), |
51 |
| - 'linear', 'log', 'symlog', 'logit']), |
52 |
| - sep, |
53 |
| - (None, "<b>Y-Axis</b>"), |
54 |
| - ('Bottom', ymin), ('Top', ymax), |
55 |
| - ('Label', axes.get_ylabel()), |
56 |
| - ('Scale', [axes.get_yscale(), |
57 |
| - 'linear', 'log', 'symlog', 'logit']), |
58 |
| - sep, |
59 |
| - ('(Re-)Generate automatic legend', False), |
60 |
| - ] |
61 |
| - |
62 |
| - # Save the unit data |
63 |
| - xunits = axes.xaxis.get_units() |
64 |
| - yunits = axes.yaxis.get_units() |
| 42 | + axis_map = axes._axis_map |
| 43 | + axis_limits = { |
| 44 | + name: tuple(convert_limits( |
| 45 | + getattr(axes, f'get_{name}lim')(), axis.converter |
| 46 | + )) |
| 47 | + for name, axis in axis_map.items() |
| 48 | + } |
| 49 | + general = [ |
| 50 | + ('Title', axes.get_title()), |
| 51 | + sep, |
| 52 | + *chain.from_iterable([ |
| 53 | + ( |
| 54 | + (None, f"<b>{name.title()}-Axis</b>"), |
| 55 | + ('Min', axis_limits[name][0]), |
| 56 | + ('Max', axis_limits[name][1]), |
| 57 | + ('Label', axis.get_label().get_text()), |
| 58 | + ('Scale', [axis.get_scale(), |
| 59 | + 'linear', 'log', 'symlog', 'logit']), |
| 60 | + sep, |
| 61 | + ) |
| 62 | + for name, axis in axis_map.items() |
| 63 | + ]), |
| 64 | + ('(Re-)Generate automatic legend', False), |
| 65 | + ] |
| 66 | + |
| 67 | + # Save the converter and unit data |
| 68 | + axis_converter = { |
| 69 | + name: axis.converter |
| 70 | + for name, axis in axis_map.items() |
| 71 | + } |
| 72 | + axis_units = { |
| 73 | + name: axis.get_units() |
| 74 | + for name, axis in axis_map.items() |
| 75 | + } |
65 | 76 |
|
66 | 77 | # Get / Curves
|
67 | 78 | labeled_lines = []
|
@@ -165,37 +176,35 @@ def prepare_data(d, init):
|
165 | 176 |
|
166 | 177 | def apply_callback(data):
|
167 | 178 | """A callback to apply changes."""
|
168 |
| - orig_xlim = axes.get_xlim() |
169 |
| - orig_ylim = axes.get_ylim() |
| 179 | + orig_limits = { |
| 180 | + name: getattr(axes, f"get_{name}lim")() |
| 181 | + for name in axis_map |
| 182 | + } |
170 | 183 |
|
171 | 184 | general = data.pop(0)
|
172 | 185 | curves = data.pop(0) if has_curve else []
|
173 | 186 | mappables = data.pop(0) if has_sm else []
|
174 | 187 | if data:
|
175 | 188 | raise ValueError("Unexpected field")
|
176 | 189 |
|
177 |
| - # Set / General |
178 |
| - (title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale, |
179 |
| - generate_legend) = general |
| 190 | + title = general.pop(0) |
| 191 | + axes.set_title(title) |
| 192 | + generate_legend = general.pop() |
| 193 | + |
| 194 | + for i, (name, axis) in enumerate(axis_map.items()): |
| 195 | + axis_min = general[4*i] |
| 196 | + axis_max = general[4*i + 1] |
| 197 | + axis_label = general[4*i + 2] |
| 198 | + axis_scale = general[4*i + 3] |
| 199 | + if axis.get_scale() != axis_scale: |
| 200 | + getattr(axes, f"set_{name}scale")(axis_scale) |
180 | 201 |
|
181 |
| - if axes.get_xscale() != xscale: |
182 |
| - axes.set_xscale(xscale) |
183 |
| - if axes.get_yscale() != yscale: |
184 |
| - axes.set_yscale(yscale) |
| 202 | + axis._set_lim(axis_min, axis_max, auto=False) |
| 203 | + axis.set_label_text(axis_label) |
185 | 204 |
|
186 |
| - axes.set_title(title) |
187 |
| - axes.set_xlim(xmin, xmax) |
188 |
| - axes.set_xlabel(xlabel) |
189 |
| - axes.set_ylim(ymin, ymax) |
190 |
| - axes.set_ylabel(ylabel) |
191 |
| - |
192 |
| - # Restore the unit data |
193 |
| - axes.xaxis.converter = xconverter |
194 |
| - axes.yaxis.converter = yconverter |
195 |
| - axes.xaxis.set_units(xunits) |
196 |
| - axes.yaxis.set_units(yunits) |
197 |
| - axes.xaxis._update_axisinfo() |
198 |
| - axes.yaxis._update_axisinfo() |
| 205 | + # Restore the unit data |
| 206 | + axis.converter = axis_converter[name] |
| 207 | + axis.set_units(axis_units[name]) |
199 | 208 |
|
200 | 209 | # Set / Curves
|
201 | 210 | for index, curve in enumerate(curves):
|
@@ -242,8 +251,10 @@ def apply_callback(data):
|
242 | 251 | # Redraw
|
243 | 252 | figure = axes.get_figure()
|
244 | 253 | figure.canvas.draw()
|
245 |
| - if not (axes.get_xlim() == orig_xlim and axes.get_ylim() == orig_ylim): |
246 |
| - figure.canvas.toolbar.push_current() |
| 254 | + for name in axis_map: |
| 255 | + if getattr(axes, f"get_{name}lim")() != orig_limits[name]: |
| 256 | + figure.canvas.toolbar.push_current() |
| 257 | + break |
247 | 258 |
|
248 | 259 | _formlayout.fedit(
|
249 | 260 | datalist, title="Figure options", parent=parent,
|
|
0 commit comments