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

Skip to content

Commit 7512f67

Browse files
committed
Also convert datasets (optionally) in _process_unit_info.
1 parent 0bddb69 commit 7512f67

File tree

4 files changed

+56
-66
lines changed

4 files changed

+56
-66
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,7 @@ def axhline(self, y=0, xmin=0, xmax=1, **kwargs):
709709
ymin, ymax = self.get_ybound()
710710

711711
# Strip away the units for comparison with non-unitized bounds.
712-
self._process_unit_info({"y": y}, kwargs)
713-
yy = self.convert_yunits(y)
712+
yy, = self._process_unit_info({"y": y}, kwargs)
714713
scaley = (yy < ymin) or (yy > ymax)
715714

716715
trans = self.get_yaxis_transform(which='grid')
@@ -777,8 +776,7 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
777776
xmin, xmax = self.get_xbound()
778777

779778
# Strip away the units for comparison with non-unitized bounds.
780-
self._process_unit_info({"x": x}, kwargs)
781-
xx = self.convert_xunits(x)
779+
xx, = self._process_unit_info({"x": x}, kwargs)
782780
scalex = (xx < xmin) or (xx > xmax)
783781

784782
trans = self.get_xaxis_transform(which='grid')
@@ -918,11 +916,9 @@ def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs):
918916
self._check_no_units([xmin, xmax], ['xmin', 'xmax'])
919917
trans = self.get_yaxis_transform(which='grid')
920918

921-
self._process_unit_info({"x": [xmin, xmax], "y": [ymin, ymax]}, kwargs)
922-
923919
# first we need to strip away the units
924-
xmin, xmax = self.convert_xunits([xmin, xmax])
925-
ymin, ymax = self.convert_yunits([ymin, ymax])
920+
(xmin, xmax), (ymin, ymax) = self._process_unit_info(
921+
{"x": [xmin, xmax], "y": [ymin, ymax]}, kwargs)
926922

927923
verts = (xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)
928924
p = mpatches.Polygon(verts, **kwargs)
@@ -978,11 +974,9 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs):
978974
self._check_no_units([ymin, ymax], ['ymin', 'ymax'])
979975
trans = self.get_xaxis_transform(which='grid')
980976

981-
self._process_unit_info({"x": [xmin, xmax], "y": [ymin, ymax]}, kwargs)
982-
983977
# first we need to strip away the units
984-
xmin, xmax = self.convert_xunits([xmin, xmax])
985-
ymin, ymax = self.convert_yunits([ymin, ymax])
978+
(xmin, xmax), (ymin, ymax) = self._process_unit_info(
979+
{"x": [xmin, xmax], "y": [ymin, ymax]}, kwargs)
986980

987981
verts = [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)]
988982
p = mpatches.Polygon(verts, **kwargs)
@@ -1028,10 +1022,8 @@ def hlines(self, y, xmin, xmax, colors=None, linestyles='solid',
10281022
"""
10291023

10301024
# We do the conversion first since not all unitized data is uniform
1031-
self._process_unit_info({"x": [xmin, xmax], "y": y}, kwargs)
1032-
y = self.convert_yunits(y)
1033-
xmin = self.convert_xunits(xmin)
1034-
xmax = self.convert_xunits(xmax)
1025+
(xmin, xmax), y = self._process_unit_info(
1026+
{"x": [xmin, xmax], "y": y}, kwargs)
10351027

10361028
if not np.iterable(y):
10371029
y = [y]
@@ -1107,10 +1099,8 @@ def vlines(self, x, ymin, ymax, colors=None, linestyles='solid',
11071099
"""
11081100

11091101
# We do the conversion first since not all unitized data is uniform
1110-
self._process_unit_info({"x": x, "y": [ymin, ymax]}, kwargs)
1111-
x = self.convert_xunits(x)
1112-
ymin = self.convert_yunits(ymin)
1113-
ymax = self.convert_yunits(ymax)
1102+
x, (ymin, ymax) = self._process_unit_info(
1103+
{"x": x, "y": [ymin, ymax]}, kwargs)
11141104

11151105
if not np.iterable(x):
11161106
x = [x]
@@ -1248,13 +1238,9 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
12481238
--------
12491239
.. plot:: gallery/lines_bars_and_markers/eventplot_demo.py
12501240
"""
1251-
self._process_unit_info(
1252-
{"x": positions, "y": [lineoffsets, linelengths]}, kwargs)
1253-
12541241
# We do the conversion first since not all unitized data is uniform
1255-
positions = self.convert_xunits(positions)
1256-
lineoffsets = self.convert_yunits(lineoffsets)
1257-
linelengths = self.convert_yunits(linelengths)
1242+
positions, (lineoffsets, linelengths) = self._process_unit_info(
1243+
{"x": positions, "y": [lineoffsets, linelengths]}, kwargs)
12581244

12591245
if not np.iterable(positions):
12601246
positions = [positions]
@@ -2276,11 +2262,13 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
22762262
x = 0
22772263

22782264
if orientation == 'vertical':
2279-
self._process_unit_info({"x": x, "y": height}, kwargs)
2265+
self._process_unit_info(
2266+
{"x": x, "y": height}, kwargs, convert=False)
22802267
if log:
22812268
self.set_yscale('log', nonpositive='clip')
22822269
elif orientation == 'horizontal':
2283-
self._process_unit_info({"x": width, "y": y}, kwargs)
2270+
self._process_unit_info(
2271+
{"x": width, "y": y}, kwargs, convert=False)
22842272
if log:
22852273
self.set_xscale('log', nonpositive='clip')
22862274

@@ -2560,7 +2548,8 @@ def broken_barh(self, xranges, yrange, **kwargs):
25602548
ydata = cbook.safe_first_element(yrange)
25612549
else:
25622550
ydata = None
2563-
self._process_unit_info({"x": xdata, "y": ydata}, kwargs)
2551+
self._process_unit_info(
2552+
{"x": xdata, "y": ydata}, kwargs, convert=False)
25642553
xranges_conv = []
25652554
for xr in xranges:
25662555
if len(xr) != 2:
@@ -2680,13 +2669,9 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
26802669
locs, heads, *args = args
26812670

26822671
if orientation == 'vertical':
2683-
self._process_unit_info({"x": locs, "y": heads})
2684-
locs = self.convert_xunits(locs)
2685-
heads = self.convert_yunits(heads)
2672+
locs, heads = self._process_unit_info({"x": locs, "y": heads})
26862673
else:
2687-
self._process_unit_info({"x": heads, "y": locs})
2688-
heads = self.convert_xunits(heads)
2689-
locs = self.convert_yunits(locs)
2674+
heads, locs = self._process_unit_info({"x": heads, "y": locs})
26902675

26912676
# defaults for formats
26922677
if linefmt is None:
@@ -3170,7 +3155,7 @@ def errorbar(self, x, y, yerr=None, xerr=None,
31703155
if int(offset) != offset:
31713156
raise ValueError("errorevery's starting index must be an integer")
31723157

3173-
self._process_unit_info({"x": x, "y": y}, kwargs)
3158+
self._process_unit_info({"x": x, "y": y}, kwargs, convert=False)
31743159

31753160
# Make sure all the args are iterable; use lists not arrays to preserve
31763161
# units.
@@ -4337,9 +4322,7 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
43374322
"""
43384323
# Process **kwargs to handle aliases, conflicts with explicit kwargs:
43394324

4340-
self._process_unit_info({"x": x, "y": y}, kwargs)
4341-
x = self.convert_xunits(x)
4342-
y = self.convert_yunits(y)
4325+
x, y = self._process_unit_info({"x": x, "y": y}, kwargs)
43434326

43444327
# np.ma.ravel yields an ndarray, not a masked array,
43454328
# unless its argument is a masked array.
@@ -4568,7 +4551,7 @@ def reduce_C_function(C: array) -> float
45684551
%(PolyCollection)s
45694552
45704553
"""
4571-
self._process_unit_info({"x": x, "y": y}, kwargs)
4554+
self._process_unit_info({"x": x, "y": y}, kwargs, convert=False)
45724555

45734556
x, y, C = cbook.delete_masked_points(x, y, C)
45744557

@@ -4917,9 +4900,7 @@ def quiverkey(self, Q, X, Y, U, label, **kw):
49174900
def _quiver_units(self, args, kw):
49184901
if len(args) > 3:
49194902
x, y = args[0:2]
4920-
self._process_unit_info({"x": x, "y": y}, kw)
4921-
x = self.convert_xunits(x)
4922-
y = self.convert_yunits(y)
4903+
x, y = self._process_unit_info({"x": x, "y": y}, kw)
49234904
return (x, y) + args[2:]
49244905
return args
49254906

@@ -5105,8 +5086,9 @@ def _fill_between_x_or_y(
51055086
self._get_patches_for_fill.get_next_color()
51065087

51075088
# Handle united data, such as dates
5108-
self._process_unit_info({ind_dir: ind, dep_dir: dep1}, kwargs)
5109-
self._process_unit_info({dep_dir: dep2})
5089+
self._process_unit_info({ind_dir: ind, dep_dir: dep1}, kwargs,
5090+
convert=False)
5091+
self._process_unit_info({dep_dir: dep2}, convert=False)
51105092

51115093
# Convert the arrays so we can work with them
51125094
ind = ma.masked_invalid(getattr(self, f"convert_{ind_dir}units")(ind))
@@ -5728,9 +5710,7 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
57285710
Ny, Nx = X.shape
57295711

57305712
# unit conversion allows e.g. datetime objects as axis values
5731-
self._process_unit_info({"x": X, "y": Y}, kwargs)
5732-
X = self.convert_xunits(X)
5733-
Y = self.convert_yunits(Y)
5713+
X, Y = self._process_unit_info({"x": X, "y": Y}, kwargs)
57345714

57355715
# convert to MA, if necessary.
57365716
C = ma.asarray(C)
@@ -6005,9 +5985,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
60055985
X = X.ravel()
60065986
Y = Y.ravel()
60075987
# unit conversion allows e.g. datetime objects as axis values
6008-
self._process_unit_info({"x": X, "y": Y}, kwargs)
6009-
X = self.convert_xunits(X)
6010-
Y = self.convert_yunits(Y)
5988+
X, Y = self._process_unit_info({"x": X, "y": Y}, kwargs)
60115989

60125990
# convert to one dimensional arrays
60135991
C = C.ravel()
@@ -6489,7 +6467,8 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
64896467
# Process unit information
64906468
# Unit conversion is done individually on each dataset
64916469
self._process_unit_info(
6492-
{"x" if orientation == "vertical" else "y": x[0]}, kwargs)
6470+
{"x" if orientation == "vertical" else "y": x[0]}, kwargs,
6471+
convert=False)
64936472
x = [self.convert_xunits(xi) for xi in x]
64946473

64956474
if bin_range is not None:
@@ -6777,9 +6756,8 @@ def stairs(self, values, edges=None, *,
67776756
if edges is None:
67786757
edges = np.arange(len(values) + 1)
67796758

6780-
self._process_unit_info({"x": edges, "y": values}, kwargs)
6781-
edges = self.convert_xunits(edges)
6782-
values = self.convert_yunits(values)
6759+
edges, values = self._process_unit_info(
6760+
{"x": edges, "y": values}, kwargs)
67836761

67846762
patch = mpatches.StepPatch(values,
67856763
edges,

lib/matplotlib/axes/_base.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,9 +2208,10 @@ def update_datalim_bounds(self, bounds):
22082208
"""
22092209
self.dataLim.set(mtransforms.Bbox.union([self.dataLim, bounds]))
22102210

2211-
def _process_unit_info(self, datasets=None, kwargs=None):
2211+
def _process_unit_info(self, datasets=None, kwargs=None, *, convert=True):
22122212
"""
2213-
Set axis units based on *datasets* and *kwargs*.
2213+
Set axis units based on *datasets* and *kwargs*, and optionally apply
2214+
unit conversions to *datasets*.
22142215
22152216
Parameters
22162217
----------
@@ -2219,8 +2220,18 @@ def _process_unit_info(self, datasets=None, kwargs=None):
22192220
kwargs : dict
22202221
Other parameters from which unit info may be popped. Note that
22212222
this dict is mutated in-place!
2223+
Other parameters from which unit info may be popped.
2224+
convert : bool; default: True
2225+
Whether to return the original datasets or the converted ones.
2226+
2227+
Returns
2228+
-------
2229+
list
2230+
Either the original datasets (``datasets.values()``) if *convert*
2231+
is False, or the converted ones if *convert* is True (the default).
22222232
"""
22232233
datasets = datasets or {}
2234+
retval = []
22242235
axis_map = self._get_axis_map()
22252236
for axis_name in datasets:
22262237
if axis_name not in axis_map:
@@ -2246,7 +2257,9 @@ def _process_unit_info(self, datasets=None, kwargs=None):
22462257
# we need to update.
22472258
if data is not None:
22482259
axis.update_units(data)
2249-
return kwargs
2260+
if data is not None:
2261+
retval.append(axis.convert_units(data) if convert else data)
2262+
return retval
22502263

22512264
def in_axes(self, mouseevent):
22522265
"""
@@ -3398,7 +3411,7 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
33983411
raise TypeError('Cannot pass both `xmax` and `right`')
33993412
right = xmax
34003413

3401-
self._process_unit_info({"x": (left, right)})
3414+
self._process_unit_info({"x": (left, right)}, convert=False)
34023415
left = self._validate_converted_limits(left, self.convert_xunits)
34033416
right = self._validate_converted_limits(right, self.convert_xunits)
34043417

@@ -3723,7 +3736,7 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
37233736
raise TypeError('Cannot pass both `ymax` and `top`')
37243737
top = ymax
37253738

3726-
self._process_unit_info({"y": (bottom, top)})
3739+
self._process_unit_info({"y": (bottom, top)}, convert=False)
37273740
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
37283741
top = self._validate_converted_limits(top, self.convert_yunits)
37293742

lib/matplotlib/contour.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,9 +1475,8 @@ def _check_xyz(self, args, kwargs):
14751475
convert them to 2D using meshgrid.
14761476
"""
14771477
x, y = args[:2]
1478-
self.axes._process_unit_info({"x": x, "y": y}, kwargs)
1479-
x = self.axes.convert_xunits(x)
1480-
y = self.axes.convert_yunits(y)
1478+
x, y = self.axes._process_unit_info(
1479+
{"x": x, "y": y}, kwargs, convert=True)
14811480

14821481
x = np.asarray(x, dtype=np.float64)
14831482
y = np.asarray(y, dtype=np.float64)

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False,
724724
raise TypeError('Cannot pass both `xmax` and `right`')
725725
right = xmax
726726

727-
self._process_unit_info({"x": (left, right)})
727+
self._process_unit_info({"x": (left, right)}, convert=False)
728728
left = self._validate_converted_limits(left, self.convert_xunits)
729729
right = self._validate_converted_limits(right, self.convert_xunits)
730730

@@ -778,7 +778,7 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False,
778778
raise TypeError('Cannot pass both `ymax` and `top`')
779779
top = ymax
780780

781-
self._process_unit_info({"y": (bottom, top)})
781+
self._process_unit_info({"y": (bottom, top)}, convert=False)
782782
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
783783
top = self._validate_converted_limits(top, self.convert_yunits)
784784

@@ -833,7 +833,7 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False,
833833
raise TypeError('Cannot pass both `zmax` and `top`')
834834
top = zmax
835835

836-
self._process_unit_info({"z": (bottom, top)})
836+
self._process_unit_info({"z": (bottom, top)}, convert=False)
837837
bottom = self._validate_converted_limits(bottom, self.convert_zunits)
838838
top = self._validate_converted_limits(top, self.convert_zunits)
839839

0 commit comments

Comments
 (0)