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

Skip to content

Commit 762c767

Browse files
committed
Also convert datasets (optionally) in _process_unit_info.
1 parent 9e857f8 commit 762c767

File tree

4 files changed

+55
-60
lines changed

4 files changed

+55
-60
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 33 additions & 49 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,7 +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)
1025+
self._process_unit_info(
1026+
{"x": [xmin, xmax], "y": y}, kwargs, convert=False)
10321027
y = self.convert_yunits(y)
10331028
xmin = self.convert_xunits(xmin)
10341029
xmax = self.convert_xunits(xmax)
@@ -1107,7 +1102,8 @@ def vlines(self, x, ymin, ymax, colors=None, linestyles='solid',
11071102
"""
11081103

11091104
# We do the conversion first since not all unitized data is uniform
1110-
self._process_unit_info({"x": x, "y": [ymin, ymax]}, kwargs)
1105+
self._process_unit_info(
1106+
{"x": x, "y": [ymin, ymax]}, kwargs, convert=False)
11111107
x = self.convert_xunits(x)
11121108
ymin = self.convert_yunits(ymin)
11131109
ymax = self.convert_yunits(ymax)
@@ -1248,13 +1244,9 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
12481244
--------
12491245
.. plot:: gallery/lines_bars_and_markers/eventplot_demo.py
12501246
"""
1251-
self._process_unit_info(
1252-
{"x": positions, "y": [lineoffsets, linelengths]}, kwargs)
1253-
12541247
# 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)
1248+
positions, (lineoffsets, linelengths) = self._process_unit_info(
1249+
{"x": positions, "y": [lineoffsets, linelengths]}, kwargs)
12581250

12591251
if not np.iterable(positions):
12601252
positions = [positions]
@@ -2276,11 +2268,13 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
22762268
x = 0
22772269

22782270
if orientation == 'vertical':
2279-
self._process_unit_info({"x": x, "y": height}, kwargs)
2271+
self._process_unit_info(
2272+
{"x": x, "y": height}, kwargs, convert=False)
22802273
if log:
22812274
self.set_yscale('log', nonpositive='clip')
22822275
elif orientation == 'horizontal':
2283-
self._process_unit_info({"x": width, "y": y}, kwargs)
2276+
self._process_unit_info(
2277+
{"x": width, "y": y}, kwargs, convert=False)
22842278
if log:
22852279
self.set_xscale('log', nonpositive='clip')
22862280

@@ -2560,7 +2554,8 @@ def broken_barh(self, xranges, yrange, **kwargs):
25602554
ydata = cbook.safe_first_element(yrange)
25612555
else:
25622556
ydata = None
2563-
self._process_unit_info({"x": xdata, "y": ydata}, kwargs)
2557+
self._process_unit_info(
2558+
{"x": xdata, "y": ydata}, kwargs, convert=False)
25642559
xranges_conv = []
25652560
for xr in xranges:
25662561
if len(xr) != 2:
@@ -2680,13 +2675,9 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
26802675
locs, heads, *args = args
26812676

26822677
if orientation == 'vertical':
2683-
self._process_unit_info({"x": locs, "y": heads})
2684-
locs = self.convert_xunits(locs)
2685-
heads = self.convert_yunits(heads)
2678+
locs, heads = self._process_unit_info({"x": locs, "y": heads})
26862679
else:
2687-
self._process_unit_info({"x": heads, "y": locs})
2688-
heads = self.convert_xunits(heads)
2689-
locs = self.convert_yunits(locs)
2680+
heads, locs = self._process_unit_info({"x": heads, "y": locs})
26902681

26912682
# defaults for formats
26922683
if linefmt is None:
@@ -3170,7 +3161,7 @@ def errorbar(self, x, y, yerr=None, xerr=None,
31703161
if int(offset) != offset:
31713162
raise ValueError("errorevery's starting index must be an integer")
31723163

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

31753166
# Make sure all the args are iterable; use lists not arrays to preserve
31763167
# units.
@@ -4337,9 +4328,7 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
43374328
"""
43384329
# Process **kwargs to handle aliases, conflicts with explicit kwargs:
43394330

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

43444333
# np.ma.ravel yields an ndarray, not a masked array,
43454334
# unless its argument is a masked array.
@@ -4568,7 +4557,7 @@ def reduce_C_function(C: array) -> float
45684557
%(PolyCollection)s
45694558
45704559
"""
4571-
self._process_unit_info({"x": x, "y": y}, kwargs)
4560+
self._process_unit_info({"x": x, "y": y}, kwargs, convert=False)
45724561

45734562
x, y, C = cbook.delete_masked_points(x, y, C)
45744563

@@ -4917,9 +4906,7 @@ def quiverkey(self, Q, X, Y, U, label, **kw):
49174906
def _quiver_units(self, args, kw):
49184907
if len(args) > 3:
49194908
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)
4909+
x, y = self._process_unit_info({"x": x, "y": y}, kw)
49234910
return (x, y) + args[2:]
49244911
return args
49254912

@@ -5105,8 +5092,9 @@ def _fill_between_x_or_y(
51055092
self._get_patches_for_fill.get_next_color()
51065093

51075094
# 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})
5095+
self._process_unit_info({ind_dir: ind, dep_dir: dep1}, kwargs,
5096+
convert=False)
5097+
self._process_unit_info({dep_dir: dep2}, convert=False)
51105098

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

57305718
# 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)
5719+
X, Y = self._process_unit_info({"x": X, "y": Y}, kwargs)
57345720

57355721
# convert to MA, if necessary.
57365722
C = ma.asarray(C)
@@ -6005,9 +5991,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
60055991
X = X.ravel()
60065992
Y = Y.ravel()
60075993
# 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)
5994+
X, Y = self._process_unit_info({"x": X, "y": Y}, kwargs)
60115995

60125996
# convert to one dimensional arrays
60135997
C = C.ravel()
@@ -6489,7 +6473,8 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
64896473
# Process unit information
64906474
# Unit conversion is done individually on each dataset
64916475
self._process_unit_info(
6492-
{"x" if orientation == "vertical" else "y": x[0]}, kwargs)
6476+
{"x" if orientation == "vertical" else "y": x[0]}, kwargs,
6477+
convert=False)
64936478
x = [self.convert_xunits(xi) for xi in x]
64946479

64956480
if bin_range is not None:
@@ -6777,9 +6762,8 @@ def stairs(self, values, edges=None, *,
67776762
if edges is None:
67786763
edges = np.arange(len(values) + 1)
67796764

6780-
self._process_unit_info({"x": edges, "y": values}, kwargs)
6781-
edges = self.convert_xunits(edges)
6782-
values = self.convert_yunits(values)
6765+
edges, values = self._process_unit_info(
6766+
{"x": edges, "y": values}, kwargs)
67836767

67846768
patch = mpatches.StepPatch(values,
67856769
edges,

lib/matplotlib/axes/_base.py

Lines changed: 17 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
----------
@@ -2221,8 +2222,17 @@ def _process_unit_info(self, datasets=None, kwargs=None):
22212222
*yunits*, *zunits* (for 3D axes), *runits* and *thetaunits* (for
22222223
polar axes) entries) is popped, if present. Note that this dict is
22232224
mutated in-place!
2225+
convert : bool; default: True
2226+
Whether to return the original datasets or the converted ones.
2227+
2228+
Returns
2229+
-------
2230+
list
2231+
Either the original datasets (``datasets.values()``) if *convert*
2232+
is False, or the converted ones if *convert* is True (the default).
22242233
"""
22252234
datasets = datasets or {}
2235+
retval = []
22262236
axis_map = self._get_axis_map()
22272237
for axis_name in datasets:
22282238
if axis_name not in axis_map:
@@ -2248,7 +2258,9 @@ def _process_unit_info(self, datasets=None, kwargs=None):
22482258
# we need to update.
22492259
if data is not None:
22502260
axis.update_units(data)
2251-
return kwargs
2261+
if data is not None:
2262+
retval.append(axis.convert_units(data) if convert else data)
2263+
return retval
22522264

22532265
def in_axes(self, mouseevent):
22542266
"""
@@ -3400,7 +3412,7 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
34003412
raise TypeError('Cannot pass both `xmax` and `right`')
34013413
right = xmax
34023414

3403-
self._process_unit_info({"x": (left, right)})
3415+
self._process_unit_info({"x": (left, right)}, convert=False)
34043416
left = self._validate_converted_limits(left, self.convert_xunits)
34053417
right = self._validate_converted_limits(right, self.convert_xunits)
34063418

@@ -3725,7 +3737,7 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
37253737
raise TypeError('Cannot pass both `ymax` and `top`')
37263738
top = ymax
37273739

3728-
self._process_unit_info({"y": (bottom, top)})
3740+
self._process_unit_info({"y": (bottom, top)}, convert=False)
37293741
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
37303742
top = self._validate_converted_limits(top, self.convert_yunits)
37313743

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)