29
29
import matplotlib .ticker as mticker
30
30
import matplotlib .transforms as mtransforms
31
31
import matplotlib .tri as mtri
32
+ import matplotlib .units as munits
32
33
from matplotlib import _preprocess_data , rcParams
33
34
from matplotlib .axes ._base import _AxesBase , _process_plot_format
34
35
from matplotlib .axes ._secondary_axes import SecondaryAxis
@@ -827,10 +828,10 @@ def axhline(self, y=0, xmin=0, xmax=1, **kwargs):
827
828
828
829
>>> axhline(y=.5, xmin=0.25, xmax=0.75)
829
830
"""
831
+ self ._check_no_units ([xmin , xmax ], ['xmin' , 'xmax' ])
830
832
if "transform" in kwargs :
831
- raise ValueError (
832
- "'transform' is not allowed as a kwarg;"
833
- + "axhline generates its own transform." )
833
+ raise ValueError ("'transform' is not allowed as a keyword argument; "
834
+ "axhline generates its own transform." )
834
835
ymin , ymax = self .get_ybound ()
835
836
836
837
# We need to strip away the units for comparison with
@@ -896,11 +897,10 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
896
897
897
898
>>> axvline(x=.5, ymin=0.25, ymax=0.75)
898
899
"""
899
-
900
+ self . _check_no_units ([ ymin , ymax ], [ 'ymin' , 'ymax' ])
900
901
if "transform" in kwargs :
901
- raise ValueError (
902
- "'transform' is not allowed as a kwarg;"
903
- + "axvline generates its own transform." )
902
+ raise ValueError ("'transform' is not allowed as a keyword argument; "
903
+ "axvline generates its own transform." )
904
904
xmin , xmax = self .get_xbound ()
905
905
906
906
# We need to strip away the units for comparison with
@@ -915,6 +915,14 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
915
915
self ._request_autoscale_view (scalex = scalex , scaley = False )
916
916
return l
917
917
918
+ @staticmethod
919
+ def _check_no_units (vals , names ):
920
+ # Helper method to check that vals are not unitized
921
+ for val , name in zip (vals , names ):
922
+ if not munits ._is_natively_supported (val ):
923
+ raise ValueError (f"{ name } must be a single scalar value, "
924
+ f"but got { val } " )
925
+
918
926
@docstring .dedent_interpd
919
927
def axline (self , xy1 , xy2 = None , * , slope = None , ** kwargs ):
920
928
"""
@@ -1035,6 +1043,7 @@ def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs):
1035
1043
--------
1036
1044
axvspan : Add a vertical span across the axes.
1037
1045
"""
1046
+ self ._check_no_units ([xmin , xmax ], ['xmin' , 'xmax' ])
1038
1047
trans = self .get_yaxis_transform (which = 'grid' )
1039
1048
1040
1049
# process the unit information
@@ -1095,6 +1104,7 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs):
1095
1104
>>> axvspan(1.25, 1.55, facecolor='g', alpha=0.5)
1096
1105
1097
1106
"""
1107
+ self ._check_no_units ([ymin , ymax ], ['ymin' , 'ymax' ])
1098
1108
trans = self .get_xaxis_transform (which = 'grid' )
1099
1109
1100
1110
# process the unit information
0 commit comments