本文整理汇总了Python中matplotlib.units.ConversionInterface方法的典型用法代码示例。如果您正苦于以下问题:Python units.ConversionInterface方法的具体用法?Python units.ConversionInterface怎么用?Python units.ConversionInterface使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类matplotlib.units
的用法示例。
在下文中一共展示了units.ConversionInterface方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convert
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def convert(value, unit, axis):
"""
If *value* is not already a number or sequence of numbers,
convert it with :func:`date2num`.
The *unit* and *axis* arguments are not used.
"""
if units.ConversionInterface.is_numlike(value):
return value
return date2num(value)
示例2: convert
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def convert(value, unit, axis):
"""Convert strings in value to floats using
mapping information store in the unit object.
Parameters
----------
value : string or iterable
Value or list of values to be converted.
unit : `.UnitData`
An object mapping strings to integers.
axis : `~matplotlib.axis.Axis`
axis on which the converted value is plotted.
.. note:: *axis* is unused.
Returns
-------
mapped_value : float or ndarray[float]
"""
if unit is None:
raise ValueError(
'Missing category information for StrCategoryConverter; '
'this might be caused by unintendedly mixing categorical and '
'numeric data')
# dtype = object preserves numerical pass throughs
values = np.atleast_1d(np.array(value, dtype=object))
# pass through sequence of non binary numbers
if all((units.ConversionInterface.is_numlike(v) and
not isinstance(v, (str, bytes))) for v in values):
return np.asarray(values, dtype=float)
# force an update so it also does type checking
unit.update(values)
str2idx = np.vectorize(unit._mapping.__getitem__,
otypes=[float])
mapped_value = str2idx(values)
return mapped_value
示例3: convert
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def convert( value, unit, axis ):
""": Convert value using unit to a float. If value is a sequence, return
the converted sequence.
= INPUT VARIABLES
- value The value or list of values that need to be converted.
- unit The units to use for an axis with Epoch data.
= RETURN VALUE
- Returns the value parameter converted to floats.
"""
# Delay-load due to circular dependencies.
import matplotlib.testing.jpl_units as U
isNotEpoch = True
isDuration = False
if ( iterable(value) and not isinstance(value, str) ):
if ( len(value) == 0 ):
return []
else:
return [ EpochConverter.convert( x, unit, axis ) for x in value ]
if ( isinstance(value, U.Epoch) ):
isNotEpoch = False
elif ( isinstance(value, U.Duration) ):
isDuration = True
if ( isNotEpoch and not isDuration and
units.ConversionInterface.is_numlike( value ) ):
return value
if ( unit == None ):
unit = EpochConverter.default_units( value, axis )
if ( isDuration ):
return EpochConverter.duration2float( value )
else:
return EpochConverter.epoch2float( value, unit )
#------------------------------------------------------------------------
示例4: convert
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def convert( value, unit, axis ):
""": Convert value using unit to a float. If value is a sequence, return
the converted sequence.
= INPUT VARIABLES
- value The value or list of values that need to be converted.
- unit The units to use for an axis with Epoch data.
= RETURN VALUE
- Returns the value parameter converted to floats.
"""
# Delay-load due to circular dependencies.
import matplotlib.testing.jpl_units as U
isNotEpoch = True
isDuration = False
if ( iterable(value) and not isinstance(value, six.string_types) ):
if ( len(value) == 0 ):
return []
else:
return [ EpochConverter.convert( x, unit, axis ) for x in value ]
if ( isinstance(value, U.Epoch) ):
isNotEpoch = False
elif ( isinstance(value, U.Duration) ):
isDuration = True
if ( isNotEpoch and not isDuration and
units.ConversionInterface.is_numlike( value ) ):
return value
if ( unit == None ):
unit = EpochConverter.default_units( value, axis )
if ( isDuration ):
return EpochConverter.duration2float( value )
else:
return EpochConverter.epoch2float( value, unit )
#------------------------------------------------------------------------
示例5: convert
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def convert(value, unit, axis):
"""Converts strings in value to floats using
mapping information store in the unit object.
Parameters
----------
value : string or iterable
value or list of values to be converted
unit : :class:`.UnitData`
object string unit information for value
axis : :class:`~matplotlib.Axis.axis`
axis on which the converted value is plotted
Returns
-------
mapped_ value : float or ndarray[float]
.. note:: axis is not used in this function
"""
# dtype = object preserves numerical pass throughs
values = np.atleast_1d(np.array(value, dtype=object))
# pass through sequence of non binary numbers
if all((units.ConversionInterface.is_numlike(v) and
not isinstance(v, (str, bytes))) for v in values):
return np.asarray(values, dtype=float)
# force an update so it also does type checking
unit.update(values)
str2idx = np.vectorize(unit._mapping.__getitem__,
otypes=[float])
mapped_value = str2idx(values)
return mapped_value
示例6: convert
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def convert(value, unit, axis):
""": Convert value using unit to a float. If value is a sequence, return
the converted sequence.
= INPUT VARIABLES
- value The value or list of values that need to be converted.
- unit The units to use for an axis with Epoch data.
= RETURN VALUE
- Returns the value parameter converted to floats.
"""
# Delay-load due to circular dependencies.
import matplotlib.testing.jpl_units as U
isNotEpoch = True
isDuration = False
if iterable(value) and not isinstance(value, str):
if len(value) == 0:
return []
else:
return [EpochConverter.convert(x, unit, axis) for x in value]
if isinstance(value, U.Epoch):
isNotEpoch = False
elif isinstance(value, U.Duration):
isDuration = True
if (isNotEpoch and not isDuration and
units.ConversionInterface.is_numlike(value)):
return value
if unit is None:
unit = EpochConverter.default_units(value, axis)
if isDuration:
return EpochConverter.duration2float(value)
else:
return EpochConverter.epoch2float(value, unit)
# -----------------------------------------------------------------------
示例7: convert
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def convert(val, unit, axis):
if units.ConversionInterface.is_numlike(val):
return val
if iterable(val):
return [thisval.convert_to(unit).get_value() for thisval in val]
else:
return val.convert_to(unit).get_value()
示例8: quantity_converter
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def quantity_converter():
# Create an instance of the conversion interface and
# mock so we can check methods called
qc = munits.ConversionInterface()
def convert(value, unit, axis):
if hasattr(value, 'units'):
return value.to(unit).magnitude
elif iterable(value):
try:
return [v.to(unit).magnitude for v in value]
except AttributeError:
return [Quantity(v, axis.get_units()).to(unit).magnitude
for v in value]
else:
return Quantity(value, axis.get_units()).to(unit).magnitude
def default_units(value, axis):
if hasattr(value, 'units'):
return value.units
elif np.iterable(value):
for v in value:
if hasattr(v, 'units'):
return v.units
return None
qc.convert = MagicMock(side_effect=convert)
qc.axisinfo = MagicMock(side_effect=lambda u, a: munits.AxisInfo(label=u))
qc.default_units = MagicMock(side_effect=default_units)
return qc
# Tests that the conversion machinery works properly for classes that
# work as a facade over numpy arrays (like pint)
示例9: convert
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def convert(value, unit, axis):
""": Convert value using unit to a float. If value is a sequence, return
the converted sequence.
= INPUT VARIABLES
- value The value or list of values that need to be converted.
- unit The units to use for an axis with Epoch data.
= RETURN VALUE
- Returns the value parameter converted to floats.
"""
# Delay-load due to circular dependencies.
import matplotlib.testing.jpl_units as U
isNotEpoch = True
isDuration = False
if np.iterable(value) and not isinstance(value, str):
if len(value) == 0:
return []
else:
return [EpochConverter.convert(x, unit, axis) for x in value]
if isinstance(value, U.Epoch):
isNotEpoch = False
elif isinstance(value, U.Duration):
isDuration = True
if (isNotEpoch and not isDuration and
units.ConversionInterface.is_numlike(value)):
return value
if unit is None:
unit = EpochConverter.default_units(value, axis)
if isDuration:
return EpochConverter.duration2float(value)
else:
return EpochConverter.epoch2float(value, unit)
示例10: convert
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def convert(value, unit, axis):
"""Converts strings in value to floats using
mapping information store in the unit object
Parameters
----------
value : string or iterable
value or list of values to be converted
unit : :class:`.UnitData`
object string unit information for value
axis : :class:`~matplotlib.Axis.axis`
axis on which the converted value is plotted
Returns
-------
mapped_ value : float or ndarray[float]
.. note:: axis is not used in this function
"""
# dtype = object preserves numerical pass throughs
values = np.atleast_1d(np.array(value, dtype=object))
# pass through sequence of non binary numbers
if all((units.ConversionInterface.is_numlike(v) and
not isinstance(v, VALID_TYPES)) for v in values):
return np.asarray(values, dtype=float)
# force an update so it also does type checking
unit.update(values)
str2idx = np.vectorize(unit._mapping.__getitem__,
otypes=[float])
mapped_value = str2idx(values)
return mapped_value
示例11: convert
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def convert( value, unit, axis ):
""": Convert value using unit to a float. If value is a sequence, return
the converted sequence.
= INPUT VARIABLES
- value The value or list of values that need to be converted.
- unit The units to use for a axis with Epoch data.
= RETURN VALUE
- Returns the value parameter converted to floats.
"""
# Delay-load due to circular dependencies.
import matplotlib.testing.jpl_units as U
isNotUnitDbl = True
if ( iterable(value) and not isinstance(value, str) ):
if ( len(value) == 0 ):
return []
else:
return [ UnitDblConverter.convert( x, unit, axis ) for x in value ]
# We need to check to see if the incoming value is actually a UnitDbl and
# set a flag. If we get an empty list, then just return an empty list.
if ( isinstance(value, U.UnitDbl) ):
isNotUnitDbl = False
# If the incoming value behaves like a number, but is not a UnitDbl,
# then just return it because we don't know how to convert it
# (or it is already converted)
if ( isNotUnitDbl and units.ConversionInterface.is_numlike( value ) ):
return value
# If no units were specified, then get the default units to use.
if ( unit == None ):
unit = UnitDblConverter.default_units( value, axis )
# Convert the incoming UnitDbl value/values to float/floats
if isinstance( axis.axes, polar.PolarAxes ) and (value.type() == "angle"):
# Guarantee that units are radians for polar plots.
return value.convert( "rad" )
return value.convert( unit )
#------------------------------------------------------------------------
示例12: convert
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def convert( value, unit, axis ):
""": Convert value using unit to a float. If value is a sequence, return
the converted sequence.
= INPUT VARIABLES
- value The value or list of values that need to be converted.
- unit The units to use for a axis with Epoch data.
= RETURN VALUE
- Returns the value parameter converted to floats.
"""
# Delay-load due to circular dependencies.
import matplotlib.testing.jpl_units as U
isNotUnitDbl = True
if ( iterable(value) and not isinstance(value, six.string_types) ):
if ( len(value) == 0 ):
return []
else:
return [ UnitDblConverter.convert( x, unit, axis ) for x in value ]
# We need to check to see if the incoming value is actually a UnitDbl and
# set a flag. If we get an empty list, then just return an empty list.
if ( isinstance(value, U.UnitDbl) ):
isNotUnitDbl = False
# If the incoming value behaves like a number, but is not a UnitDbl,
# then just return it because we don't know how to convert it
# (or it is already converted)
if ( isNotUnitDbl and units.ConversionInterface.is_numlike( value ) ):
return value
# If no units were specified, then get the default units to use.
if ( unit == None ):
unit = UnitDblConverter.default_units( value, axis )
# Convert the incoming UnitDbl value/values to float/floats
if isinstance( axis.axes, polar.PolarAxes ) and (value.type() == "angle"):
# Guarantee that units are radians for polar plots.
return value.convert( "rad" )
return value.convert( unit )
#------------------------------------------------------------------------
示例13: convert
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def convert(value, unit, axis):
""": Convert value using unit to a float. If value is a sequence, return
the converted sequence.
= INPUT VARIABLES
- value The value or list of values that need to be converted.
- unit The units to use for a axis with Epoch data.
= RETURN VALUE
- Returns the value parameter converted to floats.
"""
# Delay-load due to circular dependencies.
import matplotlib.testing.jpl_units as U
isNotUnitDbl = True
if iterable(value) and not isinstance(value, str):
if len(value) == 0:
return []
else:
return [UnitDblConverter.convert(x, unit, axis) for x in value]
# We need to check to see if the incoming value is actually a
# UnitDbl and set a flag. If we get an empty list, then just
# return an empty list.
if (isinstance(value, U.UnitDbl)):
isNotUnitDbl = False
# If the incoming value behaves like a number, but is not a UnitDbl,
# then just return it because we don't know how to convert it
# (or it is already converted)
if (isNotUnitDbl and units.ConversionInterface.is_numlike(value)):
return value
# If no units were specified, then get the default units to use.
if unit is None:
unit = UnitDblConverter.default_units(value, axis)
# Convert the incoming UnitDbl value/values to float/floats
if isinstance(axis.axes, polar.PolarAxes) and value.type() == "angle":
# Guarantee that units are radians for polar plots.
return value.convert("rad")
return value.convert(unit)
# -----------------------------------------------------------------------
示例14: convert
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def convert(value, unit, axis):
""": Convert value using unit to a float. If value is a sequence, return
the converted sequence.
= INPUT VARIABLES
- value The value or list of values that need to be converted.
- unit The units to use for a axis with Epoch data.
= RETURN VALUE
- Returns the value parameter converted to floats.
"""
# Delay-load due to circular dependencies.
import matplotlib.testing.jpl_units as U
isNotUnitDbl = True
if np.iterable(value) and not isinstance(value, str):
if len(value) == 0:
return []
else:
return [UnitDblConverter.convert(x, unit, axis) for x in value]
# We need to check to see if the incoming value is actually a
# UnitDbl and set a flag. If we get an empty list, then just
# return an empty list.
if isinstance(value, U.UnitDbl):
isNotUnitDbl = False
# If the incoming value behaves like a number, but is not a UnitDbl,
# then just return it because we don't know how to convert it
# (or it is already converted)
if isNotUnitDbl and units.ConversionInterface.is_numlike(value):
return value
# If no units were specified, then get the default units to use.
if unit is None:
unit = UnitDblConverter.default_units(value, axis)
# Convert the incoming UnitDbl value/values to float/floats
if isinstance(axis.axes, polar.PolarAxes) and value.type() == "angle":
# Guarantee that units are radians for polar plots.
return value.convert("rad")
return value.convert(unit)
示例15: test_numpy_facade
# 需要导入模块: from matplotlib import units [as 别名]
# 或者: from matplotlib.units import ConversionInterface [as 别名]
def test_numpy_facade():
# Create an instance of the conversion interface and
# mock so we can check methods called
qc = munits.ConversionInterface()
def convert(value, unit, axis):
if hasattr(value, 'units'):
return value.to(unit).magnitude
elif iterable(value):
try:
return [v.to(unit).magnitude for v in value]
except AttributeError:
return [Quantity(v, axis.get_units()).to(unit).magnitude
for v in value]
else:
return Quantity(value, axis.get_units()).to(unit).magnitude
qc.convert = MagicMock(side_effect=convert)
qc.axisinfo = MagicMock(side_effect=lambda u, a: munits.AxisInfo(label=u))
qc.default_units = MagicMock(side_effect=lambda x, a: x.units)
# Register the class
munits.registry[Quantity] = qc
# Simple test
y = Quantity(np.linspace(0, 30), 'miles')
x = Quantity(np.linspace(0, 5), 'hours')
fig, ax = plt.subplots()
fig.subplots_adjust(left=0.15) # Make space for label
ax.plot(x, y, 'tab:blue')
ax.axhline(Quantity(26400, 'feet'), color='tab:red')
ax.axvline(Quantity(120, 'minutes'), color='tab:green')
ax.yaxis.set_units('inches')
ax.xaxis.set_units('seconds')
assert qc.convert.called
assert qc.axisinfo.called
assert qc.default_units.called
# Tests gh-8908