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

Skip to content

Commit b543bc0

Browse files
committed
Also convert datasets (optionally) in _process_unit_info.
1 parent d4e0beb commit b543bc0

File tree

4 files changed

+54
-63
lines changed

4 files changed

+54
-63
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,7 @@ def axhline(self, y=0, xmin=0, xmax=1, **kwargs):
839839
ymin, ymax = self.get_ybound()
840840

841841
# Strip away the units for comparison with non-unitized bounds.
842-
self._process_unit_info({"y": y}, kwargs)
843-
yy = self.convert_yunits(y)
842+
yy, = self._process_unit_info({"y": y}, kwargs)
844843
scaley = (yy < ymin) or (yy > ymax)
845844

846845
trans = self.get_yaxis_transform(which='grid')
@@ -907,8 +906,7 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
907906
xmin, xmax = self.get_xbound()
908907

909908
# Strip away the units for comparison with non-unitized bounds.
910-
self._process_unit_info({"x": x}, kwargs)
911-
xx = self.convert_xunits(x)
909+
xx, = self._process_unit_info({"x": x}, kwargs)
912910
scalex = (xx < xmin) or (xx > xmax)
913911

914912
trans = self.get_xaxis_transform(which='grid')
@@ -1048,11 +1046,9 @@ def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs):
10481046
self._check_no_units([xmin, xmax], ['xmin', 'xmax'])
10491047
trans = self.get_yaxis_transform(which='grid')
10501048

1051-
self._process_unit_info({"x": [xmin, xmax], "y": [ymin, ymax]}, kwargs)
1052-
10531049
# first we need to strip away the units
1054-
xmin, xmax = self.convert_xunits([xmin, xmax])
1055-
ymin, ymax = self.convert_yunits([ymin, ymax])
1050+
(xmin, xmax), (ymin, ymax) = self._process_unit_info(
1051+
{"x": [xmin, xmax], "y": [ymin, ymax]}, kwargs)
10561052

10571053
verts = (xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)
10581054
p = mpatches.Polygon(verts, **kwargs)
@@ -1108,11 +1104,9 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs):
11081104
self._check_no_units([ymin, ymax], ['ymin', 'ymax'])
11091105
trans = self.get_xaxis_transform(which='grid')
11101106

1111-
self._process_unit_info({"x": [xmin, xmax], "y": [ymin, ymax]}, kwargs)
1112-
11131107
# first we need to strip away the units
1114-
xmin, xmax = self.convert_xunits([xmin, xmax])
1115-
ymin, ymax = self.convert_yunits([ymin, ymax])
1108+
(xmin, xmax), (ymin, ymax) = self._process_unit_info(
1109+
{"x": [xmin, xmax], "y": [ymin, ymax]}, kwargs)
11161110

11171111
verts = [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)]
11181112
p = mpatches.Polygon(verts, **kwargs)
@@ -1158,10 +1152,8 @@ def hlines(self, y, xmin, xmax, colors=None, linestyles='solid',
11581152
"""
11591153

11601154
# We do the conversion first since not all unitized data is uniform
1161-
self._process_unit_info({"x": [xmin, xmax], "y": y}, kwargs)
1162-
y = self.convert_yunits(y)
1163-
xmin = self.convert_xunits(xmin)
1164-
xmax = self.convert_xunits(xmax)
1155+
(xmin, xmax), y = self._process_unit_info(
1156+
{"x": [xmin, xmax], "y": y}, kwargs)
11651157

11661158
if not np.iterable(y):
11671159
y = [y]
@@ -1239,10 +1231,8 @@ def vlines(self, x, ymin, ymax, colors=None, linestyles='solid',
12391231
"""
12401232

12411233
# We do the conversion first since not all unitized data is uniform
1242-
self._process_unit_info({"x": x, "y": [ymin, ymax]}, kwargs)
1243-
x = self.convert_xunits(x)
1244-
ymin = self.convert_yunits(ymin)
1245-
ymax = self.convert_yunits(ymax)
1234+
x, (ymin, ymax) = self._process_unit_info(
1235+
{"x": x, "y": [ymin, ymax]}, kwargs)
12461236

12471237
if not np.iterable(x):
12481238
x = [x]
@@ -1380,13 +1370,9 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
13801370
--------
13811371
.. plot:: gallery/lines_bars_and_markers/eventplot_demo.py
13821372
"""
1383-
self._process_unit_info(
1384-
{"x": positions, "y": [lineoffsets, linelengths]}, kwargs)
1385-
13861373
# We do the conversion first since not all unitized data is uniform
1387-
positions = self.convert_xunits(positions)
1388-
lineoffsets = self.convert_yunits(lineoffsets)
1389-
linelengths = self.convert_yunits(linelengths)
1374+
positions, (lineoffsets, linelengths) = self._process_unit_info(
1375+
{"x": positions, "y": [lineoffsets, linelengths]}, kwargs)
13901376

13911377
if not np.iterable(positions):
13921378
positions = [positions]
@@ -2409,11 +2395,13 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
24092395
x = 0
24102396

24112397
if orientation == 'vertical':
2412-
self._process_unit_info({"x": x, "y": height}, kwargs)
2398+
self._process_unit_info(
2399+
{"x": x, "y": height}, kwargs, convert=False)
24132400
if log:
24142401
self.set_yscale('log', nonpositive='clip')
24152402
elif orientation == 'horizontal':
2416-
self._process_unit_info({"x": width, "y": y}, kwargs)
2403+
self._process_unit_info(
2404+
{"x": width, "y": y}, kwargs, convert=False)
24172405
if log:
24182406
self.set_xscale('log', nonpositive='clip')
24192407

@@ -2693,7 +2681,8 @@ def broken_barh(self, xranges, yrange, **kwargs):
26932681
ydata = cbook.safe_first_element(yrange)
26942682
else:
26952683
ydata = None
2696-
self._process_unit_info({"x": xdata, "y": ydata}, kwargs)
2684+
self._process_unit_info(
2685+
{"x": xdata, "y": ydata}, kwargs, convert=False)
26972686
xranges_conv = []
26982687
for xr in xranges:
26992688
if len(xr) != 2:
@@ -2814,13 +2803,9 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
28142803
locs, heads, *args = args
28152804

28162805
if orientation == 'vertical':
2817-
self._process_unit_info({"x": locs, "y": heads})
2818-
locs = self.convert_xunits(locs)
2819-
heads = self.convert_yunits(heads)
2806+
locs, heads = self._process_unit_info({"x": locs, "y": heads})
28202807
else:
2821-
self._process_unit_info({"x": heads, "y": locs})
2822-
heads = self.convert_xunits(heads)
2823-
locs = self.convert_yunits(locs)
2808+
heads, locs = self._process_unit_info({"x": heads, "y": locs})
28242809

28252810
# defaults for formats
28262811
if linefmt is None:
@@ -3304,7 +3289,7 @@ def errorbar(self, x, y, yerr=None, xerr=None,
33043289
if int(offset) != offset:
33053290
raise ValueError("errorevery's starting index must be an integer")
33063291

3307-
self._process_unit_info({"x": x, "y": y}, kwargs)
3292+
self._process_unit_info({"x": x, "y": y}, kwargs, convert=False)
33083293

33093294
# Make sure all the args are iterable; use lists not arrays to preserve
33103295
# units.
@@ -4472,9 +4457,7 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
44724457
"""
44734458
# Process **kwargs to handle aliases, conflicts with explicit kwargs:
44744459

4475-
self._process_unit_info({"x": x, "y": y}, kwargs)
4476-
x = self.convert_xunits(x)
4477-
y = self.convert_yunits(y)
4460+
x, y = self._process_unit_info({"x": x, "y": y}, kwargs)
44784461

44794462
# np.ma.ravel yields an ndarray, not a masked array,
44804463
# unless its argument is a masked array.
@@ -4703,7 +4686,7 @@ def reduce_C_function(C: array) -> float
47034686
%(PolyCollection)s
47044687
47054688
"""
4706-
self._process_unit_info({"x": x, "y": y}, kwargs)
4689+
self._process_unit_info({"x": x, "y": y}, kwargs, convert=False)
47074690

47084691
x, y, C = cbook.delete_masked_points(x, y, C)
47094692

@@ -5052,9 +5035,7 @@ def quiverkey(self, Q, X, Y, U, label, **kw):
50525035
def _quiver_units(self, args, kw):
50535036
if len(args) > 3:
50545037
x, y = args[0:2]
5055-
self._process_unit_info({"x": x, "y": y}, kw)
5056-
x = self.convert_xunits(x)
5057-
y = self.convert_yunits(y)
5038+
x, y = self._process_unit_info({"x": x, "y": y}, kw)
50585039
return (x, y) + args[2:]
50595040
return args
50605041

@@ -5240,8 +5221,9 @@ def _fill_between_x_or_y(
52405221
self._get_patches_for_fill.get_next_color()
52415222

52425223
# Handle united data, such as dates
5243-
self._process_unit_info({ind_dir: ind, dep_dir: dep1}, kwargs)
5244-
self._process_unit_info({dep_dir: dep2})
5224+
self._process_unit_info({ind_dir: ind, dep_dir: dep1}, kwargs,
5225+
convert=False)
5226+
self._process_unit_info({dep_dir: dep2}, convert=False)
52455227

52465228
# Convert the arrays so we can work with them
52475229
ind = ma.masked_invalid(getattr(self, f"convert_{ind_dir}units")(ind))
@@ -5855,9 +5837,7 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
58555837
Ny, Nx = X.shape
58565838

58575839
# unit conversion allows e.g. datetime objects as axis values
5858-
self._process_unit_info({"x": X, "y": Y}, kwargs)
5859-
X = self.convert_xunits(X)
5860-
Y = self.convert_yunits(Y)
5840+
X, Y = self._process_unit_info({"x": X, "y": Y}, kwargs)
58615841

58625842
# convert to MA, if necessary.
58635843
C = ma.asarray(C)
@@ -6132,9 +6112,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
61326112
X = X.ravel()
61336113
Y = Y.ravel()
61346114
# unit conversion allows e.g. datetime objects as axis values
6135-
self._process_unit_info({"x": X, "y": Y}, kwargs)
6136-
X = self.convert_xunits(X)
6137-
Y = self.convert_yunits(Y)
6115+
X, Y = self._process_unit_info({"x": X, "y": Y}, kwargs)
61386116

61396117
# convert to one dimensional arrays
61406118
C = C.ravel()
@@ -6617,7 +6595,8 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
66176595
# Process unit information
66186596
# Unit conversion is done individually on each dataset
66196597
self._process_unit_info(
6620-
{"x" if orientation == "vertical" else "y": x[0]}, kwargs)
6598+
{"x" if orientation == "vertical" else "y": x[0]}, kwargs,
6599+
convert=False)
66216600
x = [self.convert_xunits(xi) for xi in x]
66226601

66236602
if bin_range is not None:

lib/matplotlib/axes/_base.py

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

2178-
def _process_unit_info(self, datasets=None, kwargs=None):
2178+
def _process_unit_info(self, datasets=None, kwargs=None, *, convert=True):
21792179
"""
2180-
Set axis units based on *datasets* and *kwargs*.
2180+
Set axis units based on *datasets* and *kwargs*, and optionally apply
2181+
unit conversions to *datasets*.
21812182
21822183
Parameters
21832184
----------
@@ -2186,8 +2187,18 @@ def _process_unit_info(self, datasets=None, kwargs=None):
21862187
kwargs : dict
21872188
Other parameters from which unit info may be popped. Note that
21882189
this dict is mutated in-place!
2190+
Other parameters from which unit info may be popped.
2191+
convert : bool; default: True
2192+
Whether to return the original datasets or the converted ones.
2193+
2194+
Returns
2195+
-------
2196+
list
2197+
Either the original datasets (``datasets.values()``) if *convert*
2198+
is False, or the converted ones if *convert* is True (the default).
21892199
"""
21902200
datasets = datasets or {}
2201+
retval = []
21912202
axis_map = self._get_axis_map()
21922203
for axis_name in datasets:
21932204
if axis_name not in axis_map:
@@ -2213,7 +2224,9 @@ def _process_unit_info(self, datasets=None, kwargs=None):
22132224
# we need to update.
22142225
if data is not None:
22152226
axis.update_units(data)
2216-
return kwargs
2227+
if data is not None:
2228+
retval.append(axis.convert_units(data) if convert else data)
2229+
return retval
22172230

22182231
def in_axes(self, mouseevent):
22192232
"""
@@ -3311,7 +3324,7 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
33113324
raise TypeError('Cannot pass both `xmax` and `right`')
33123325
right = xmax
33133326

3314-
self._process_unit_info({"x": (left, right)})
3327+
self._process_unit_info({"x": (left, right)}, convert=False)
33153328
left = self._validate_converted_limits(left, self.convert_xunits)
33163329
right = self._validate_converted_limits(right, self.convert_xunits)
33173330

@@ -3582,7 +3595,7 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
35823595
raise TypeError('Cannot pass both `ymax` and `top`')
35833596
top = ymax
35843597

3585-
self._process_unit_info({"y": (bottom, top)})
3598+
self._process_unit_info({"y": (bottom, top)}, convert=False)
35863599
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
35873600
top = self._validate_converted_limits(top, self.convert_yunits)
35883601

lib/matplotlib/contour.py

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

14811480
x = np.asarray(x, dtype=np.float64)
14821481
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
@@ -723,7 +723,7 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False,
723723
raise TypeError('Cannot pass both `xmax` and `right`')
724724
right = xmax
725725

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

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

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

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

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

0 commit comments

Comments
 (0)