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

Skip to content

Commit fc91e61

Browse files
committed
Set figure options dynamically
Previously, for the figure options, things were hard coded for the X and Y axes but were not implemented for the Z axis. With this commit, all the options in the Figure options dialogue box are generated dynamically based on the axes present in the figure. This removes all the hard coded part and should make it more modular to make further changes.
1 parent a39c9e6 commit fc91e61

File tree

1 file changed

+58
-46
lines changed

1 file changed

+58
-46
lines changed

lib/matplotlib/backends/qt_editor/figureoptions.py

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
"""Module that provides a GUI-based editor for Matplotlib's figure options."""
77

8+
from itertools import chain
89
from matplotlib import cbook, cm, colors as mcolors, markers, image as mimage
910
from matplotlib.backends.qt_compat import QtGui
1011
from matplotlib.backends.qt_editor import _formlayout
@@ -38,30 +39,41 @@ def convert_limits(lim, converter):
3839
# Cast to builtin floats as they have nicer reprs.
3940
return map(float, lim)
4041

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-
]
42+
axis_map = axes._axis_map
43+
axis_converter = {
44+
axis: getattr(getattr(axes, f'{axis}axis'), 'converter')
45+
for axis in axis_map.keys()
46+
}
47+
axis_limits = {
48+
axis: tuple(convert_limits(
49+
getattr(axes, f'get_{axis}lim')(), axis_converter[axis]
50+
))
51+
for axis in axis_map.keys()
52+
}
53+
general = [
54+
('Title', axes.get_title()),
55+
sep,
56+
]
57+
axes_info = [
58+
(
59+
(None, f"<b>{axis.upper()}-Axis</b>"),
60+
('Min', axis_limits[axis][0]),
61+
('Max', axis_limits[axis][1]),
62+
('Label', getattr(axes, f"get_{axis}label")()),
63+
('Scale', [getattr(axes, f"get_{axis}scale")(),
64+
'linear', 'log', 'symlog', 'logit']),
65+
sep,
66+
)
67+
for axis in axis_map.keys()
68+
]
69+
general.extend(chain.from_iterable(axes_info))
70+
general.append(('(Re-)Generate automatic legend', False))
6171

6272
# Save the unit data
63-
xunits = axes.xaxis.get_units()
64-
yunits = axes.yaxis.get_units()
73+
axis_units = {
74+
axis: getattr(getattr(axes, f"{axis}axis"), "get_units")()
75+
for axis in axis_map.keys()
76+
}
6577

6678
# Get / Curves
6779
labeled_lines = []
@@ -165,37 +177,35 @@ def prepare_data(d, init):
165177

166178
def apply_callback(data):
167179
"""A callback to apply changes."""
168-
orig_xlim = axes.get_xlim()
169-
orig_ylim = axes.get_ylim()
180+
orig_limits = {
181+
axis: getattr(axes, f"get_{axis}lim")()
182+
for axis in axis_map.keys()
183+
}
170184

171185
general = data.pop(0)
172186
curves = data.pop(0) if has_curve else []
173187
mappables = data.pop(0) if has_sm else []
174188
if data:
175189
raise ValueError("Unexpected field")
176190

177-
# Set / General
178-
(title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale,
179-
generate_legend) = general
191+
title = general.pop(0)
192+
axes.set_title(title)
193+
generate_legend = general.pop()
180194

181-
if axes.get_xscale() != xscale:
182-
axes.set_xscale(xscale)
183-
if axes.get_yscale() != yscale:
184-
axes.set_yscale(yscale)
195+
for i, axis in enumerate(axis_map.keys()):
196+
ax = getattr(axes, f"{axis}axis")
197+
axmin = general[4*i]
198+
axmax = general[4*i + 1]
199+
axlabel = general[4*i + 2]
200+
axscale = general[4*i + 3]
201+
if getattr(axes, f"get_{axis}scale")() != axscale:
202+
getattr(axes, f"set_{axis}scale")(axscale)
185203

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()
204+
getattr(axes, f"set_{axis}lim")(axmin, axmax)
205+
getattr(axes, f"set_{axis}label")(axlabel)
206+
setattr(ax, 'converter', axis_converter[axis])
207+
getattr(ax, 'set_units')(axis_units[axis])
208+
ax._update_axisinfo()
199209

200210
# Set / Curves
201211
for index, curve in enumerate(curves):
@@ -242,8 +252,10 @@ def apply_callback(data):
242252
# Redraw
243253
figure = axes.get_figure()
244254
figure.canvas.draw()
245-
if not (axes.get_xlim() == orig_xlim and axes.get_ylim() == orig_ylim):
246-
figure.canvas.toolbar.push_current()
255+
for axis in axis_map.keys():
256+
if getattr(axes, f"get_{axis}lim")() != orig_limits[axis]:
257+
figure.canvas.toolbar.push_current()
258+
break
247259

248260
_formlayout.fedit(
249261
datalist, title="Figure options", parent=parent,

0 commit comments

Comments
 (0)