2929import matplotlib .ticker as mticker
3030import matplotlib .transforms as mtransforms
3131import matplotlib .tri as mtri
32+ import matplotlib .units as munits
3233from matplotlib import _preprocess_data , rcParams
3334from matplotlib .axes ._base import _AxesBase , _process_plot_format
3435from matplotlib .axes ._secondary_axes import SecondaryAxis
@@ -827,10 +828,10 @@ def axhline(self, y=0, xmin=0, xmax=1, **kwargs):
827828
828829 >>> axhline(y=.5, xmin=0.25, xmax=0.75)
829830 """
831+ self ._check_no_units ([xmin , xmax ], ['xmin' , 'xmax' ])
830832 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." )
834835 ymin , ymax = self .get_ybound ()
835836
836837 # We need to strip away the units for comparison with
@@ -896,11 +897,10 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
896897
897898 >>> axvline(x=.5, ymin=0.25, ymax=0.75)
898899 """
899-
900+ self . _check_no_units ([ ymin , ymax ], [ 'ymin' , 'ymax' ])
900901 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." )
904904 xmin , xmax = self .get_xbound ()
905905
906906 # We need to strip away the units for comparison with
@@ -915,6 +915,14 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
915915 self ._request_autoscale_view (scalex = scalex , scaley = False )
916916 return l
917917
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+
918926 @docstring .dedent_interpd
919927 def axline (self , xy1 , xy2 = None , * , slope = None , ** kwargs ):
920928 """
@@ -1035,6 +1043,7 @@ def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs):
10351043 --------
10361044 axvspan : Add a vertical span across the axes.
10371045 """
1046+ self ._check_no_units ([xmin , xmax ], ['xmin' , 'xmax' ])
10381047 trans = self .get_yaxis_transform (which = 'grid' )
10391048
10401049 # process the unit information
@@ -1095,6 +1104,7 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs):
10951104 >>> axvspan(1.25, 1.55, facecolor='g', alpha=0.5)
10961105
10971106 """
1107+ self ._check_no_units ([ymin , ymax ], ['ymin' , 'ymax' ])
10981108 trans = self .get_xaxis_transform (which = 'grid' )
10991109
11001110 # process the unit information
0 commit comments