22
33import numpy as np
44
5+ from matplotlib import cbook
56import matplotlib .units as units
67import matplotlib .projections .polar as polar
78
@@ -83,35 +84,21 @@ def convert(value, unit, axis):
8384 # Delay-load due to circular dependencies.
8485 import matplotlib .testing .jpl_units as U
8586
86- isNotUnitDbl = True
87-
88- if np .iterable (value ) and not isinstance (value , str ):
89- if len (value ) == 0 :
90- return []
91- else :
92- return [UnitDblConverter .convert (x , unit , axis ) for x in value ]
93-
94- # We need to check to see if the incoming value is actually a
95- # UnitDbl and set a flag. If we get an empty list, then just
96- # return an empty list.
97- if isinstance (value , U .UnitDbl ):
98- isNotUnitDbl = False
99-
87+ if not cbook .is_scalar_or_string (value ):
88+ return [UnitDblConverter .convert (x , unit , axis ) for x in value ]
10089 # If the incoming value behaves like a number, but is not a UnitDbl,
10190 # then just return it because we don't know how to convert it
10291 # (or it is already converted)
103- if isNotUnitDbl and units .ConversionInterface .is_numlike (value ):
92+ if (units .ConversionInterface .is_numlike (value )
93+ and not isinstance (value , U .UnitDbl )):
10494 return value
105-
10695 # If no units were specified, then get the default units to use.
10796 if unit is None :
10897 unit = UnitDblConverter .default_units (value , axis )
109-
11098 # Convert the incoming UnitDbl value/values to float/floats
11199 if isinstance (axis .axes , polar .PolarAxes ) and value .type () == "angle" :
112100 # Guarantee that units are radians for polar plots.
113101 return value .convert ("rad" )
114-
115102 return value .convert (unit )
116103
117104 @staticmethod
@@ -125,10 +112,9 @@ def default_units(value, axis):
125112 - Returns the default units to use for value.
126113 Return the default unit for value, or None.
127114 """
128-
129115 # Determine the default units based on the user preferences set for
130116 # default units when printing a UnitDbl.
131- if np .iterable (value ) and not isinstance (value , str ):
132- return UnitDblConverter .default_units (value [0 ], axis )
133- else :
117+ if cbook .is_scalar_or_string (value ):
134118 return UnitDblConverter .defaults [value .type ()]
119+ else :
120+ return UnitDblConverter .default_units (value [0 ], axis )
0 commit comments