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

Skip to content

Commit 07bf0d6

Browse files
committed
Add some more unit decorators
1 parent 8bc2371 commit 07bf0d6

File tree

2 files changed

+19
-32
lines changed

2 files changed

+19
-32
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,8 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid',
868868

869869
return lines
870870

871+
@munits._accepts_units(convert_x=['positions'],
872+
convert_y=['lineoffsets', 'linelengths'])
871873
@_preprocess_data(replace_names=["positions", "lineoffsets",
872874
"linelengths", "linewidths",
873875
"colors", "linestyles"],
@@ -956,15 +958,6 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
956958
957959
.. plot:: gallery/lines_bars_and_markers/eventplot_demo.py
958960
"""
959-
self._process_unit_info(xdata=positions,
960-
ydata=[lineoffsets, linelengths],
961-
kwargs=kwargs)
962-
963-
# We do the conversion first since not all unitized data is uniform
964-
positions = self.convert_xunits(positions)
965-
lineoffsets = self.convert_yunits(lineoffsets)
966-
linelengths = self.convert_yunits(linelengths)
967-
968961
if not iterable(positions):
969962
positions = [positions]
970963
elif any(iterable(position) for position in positions):
@@ -4694,6 +4687,7 @@ def fill(self, *args, **kwargs):
46944687
self.autoscale_view()
46954688
return patches
46964689

4690+
@munits._accepts_units(convert_x=['x'], convert_y=['y1', 'y2'])
46974691
@_preprocess_data(replace_names=["x", "y1", "y2", "where"],
46984692
label_namer=None)
46994693
@docstring.dedent_interpd
@@ -4787,14 +4781,10 @@ def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
47874781
kwargs['facecolor'] = \
47884782
self._get_patches_for_fill.get_next_color()
47894783

4790-
# Handle united data, such as dates
4791-
self._process_unit_info(xdata=x, ydata=y1, kwargs=kwargs)
4792-
self._process_unit_info(ydata=y2)
4793-
47944784
# Convert the arrays so we can work with them
4795-
x = ma.masked_invalid(self.convert_xunits(x))
4796-
y1 = ma.masked_invalid(self.convert_yunits(y1))
4797-
y2 = ma.masked_invalid(self.convert_yunits(y2))
4785+
x = ma.masked_invalid(x)
4786+
y1 = ma.masked_invalid(y1)
4787+
y2 = ma.masked_invalid(y2)
47984788

47994789
for name, array in [('x', x), ('y1', y1), ('y2', y2)]:
48004790
if array.ndim > 1:
@@ -4877,6 +4867,7 @@ def get_interp_point(ind):
48774867
self.autoscale_view()
48784868
return collection
48794869

4870+
@munits._accepts_units(convert_x=['x1', 'x2'], convert_y=['y'])
48804871
@_preprocess_data(replace_names=["y", "x1", "x2", "where"],
48814872
label_namer=None)
48824873
@docstring.dedent_interpd
@@ -4970,14 +4961,10 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
49704961
kwargs['facecolor'] = \
49714962
self._get_patches_for_fill.get_next_color()
49724963

4973-
# Handle united data, such as dates
4974-
self._process_unit_info(ydata=y, xdata=x1, kwargs=kwargs)
4975-
self._process_unit_info(xdata=x2)
4976-
49774964
# Convert the arrays so we can work with them
4978-
y = ma.masked_invalid(self.convert_yunits(y))
4979-
x1 = ma.masked_invalid(self.convert_xunits(x1))
4980-
x2 = ma.masked_invalid(self.convert_xunits(x2))
4965+
y = ma.masked_invalid(y)
4966+
x1 = ma.masked_invalid(x1)
4967+
x2 = ma.masked_invalid(x2)
49814968

49824969
for name, array in [('y', y), ('x1', x1), ('x2', x2)]:
49834970
if array.ndim > 1:

lib/matplotlib/axes/_base.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import matplotlib.font_manager as font_manager
2727
import matplotlib.text as mtext
2828
import matplotlib.image as mimage
29+
import matplotlib.units as munits
2930
from matplotlib.offsetbox import OffsetBox
3031
from matplotlib.artist import allow_rasterization
3132
from matplotlib.legend import Legend
@@ -2993,7 +2994,7 @@ def get_xlim(self):
29932994
"""
29942995
return tuple(self.viewLim.intervalx)
29952996

2996-
def _validate_converted_limits(self, limit, convert):
2997+
def _validate_converted_limits(self, converted_limit):
29972998
"""
29982999
Raise ValueError if converted limits are non-finite.
29993000
@@ -3004,14 +3005,13 @@ def _validate_converted_limits(self, limit, convert):
30043005
The limit value after call to convert(), or None if limit is None.
30053006
30063007
"""
3007-
if limit is not None:
3008-
converted_limit = convert(limit)
3008+
if converted_limit is not None:
30093009
if (isinstance(converted_limit, float) and
30103010
(not np.isreal(converted_limit) or
30113011
not np.isfinite(converted_limit))):
30123012
raise ValueError("Axis limits cannot be NaN or Inf")
3013-
return converted_limit
30143013

3014+
@munits._accepts_units(convert_x=['left', 'right'])
30153015
def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
30163016
"""
30173017
Set the data limits for the x-axis
@@ -3079,9 +3079,8 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
30793079
if right is None and iterable(left):
30803080
left, right = left
30813081

3082-
self._process_unit_info(xdata=(left, right))
3083-
left = self._validate_converted_limits(left, self.convert_xunits)
3084-
right = self._validate_converted_limits(right, self.convert_xunits)
3082+
self._validate_converted_limits(left)
3083+
self._validate_converted_limits(right)
30853084

30863085
old_left, old_right = self.get_xlim()
30873086
if left is None:
@@ -3332,6 +3331,7 @@ def get_ylim(self):
33323331
"""
33333332
return tuple(self.viewLim.intervaly)
33343333

3334+
@munits._accepts_units(convert_y=['bottom', 'top'])
33353335
def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
33363336
"""
33373337
Set the data limits for the y-axis
@@ -3398,8 +3398,8 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
33983398
if top is None and iterable(bottom):
33993399
bottom, top = bottom
34003400

3401-
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
3402-
top = self._validate_converted_limits(top, self.convert_yunits)
3401+
self._validate_converted_limits(bottom)
3402+
self._validate_converted_limits(top)
34033403

34043404
old_bottom, old_top = self.get_ylim()
34053405

0 commit comments

Comments
 (0)