本文整理匯總了Python中matplotlib.cbook.safe_first_element方法的典型用法代碼示例。如果您正苦於以下問題:Python cbook.safe_first_element方法的具體用法?Python cbook.safe_first_element怎麽用?Python cbook.safe_first_element使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類matplotlib.cbook
的用法示例。
在下文中一共展示了cbook.safe_first_element方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_converter
# 需要導入模塊: from matplotlib import cbook [as 別名]
# 或者: from matplotlib.cbook import safe_first_element [as 別名]
def get_converter(self, x):
"""Get the converter interface instance for *x*, or None."""
if hasattr(x, "values"):
x = x.values # Unpack pandas Series and DataFrames.
if isinstance(x, np.ndarray):
# In case x in a masked array, access the underlying data (only its
# type matters). If x is a regular ndarray, getdata() just returns
# the array itself.
x = np.ma.getdata(x).ravel()
# If there are no elements in x, infer the units from its dtype
if not x.size:
return self.get_converter(np.array([0], dtype=x.dtype))
try: # Look up in the cache.
return self[type(x)]
except KeyError:
try: # If cache lookup fails, look up based on first element...
first = cbook.safe_first_element(x)
except (TypeError, StopIteration):
pass
else:
# ... and avoid infinite recursion for pathological iterables
# where indexing returns instances of the same iterable class.
if type(first) is not type(x):
return self.get_converter(first)
return None
示例2: default_units
# 需要導入模塊: from matplotlib import cbook [as 別名]
# 或者: from matplotlib.cbook import safe_first_element [as 別名]
def default_units(x, axis):
"""
Return the tzinfo instance of *x* or of its first element, or None
"""
if isinstance(x, np.ndarray):
x = x.ravel()
try:
x = cbook.safe_first_element(x)
except (TypeError, StopIteration):
pass
try:
return x.tzinfo
except AttributeError:
pass
return None
示例3: test_flatiter
# 需要導入模塊: from matplotlib import cbook [as 別名]
# 或者: from matplotlib.cbook import safe_first_element [as 別名]
def test_flatiter():
x = np.arange(5)
it = x.flat
assert 0 == next(it)
assert 1 == next(it)
ret = cbook.safe_first_element(it)
assert ret == 0
assert 0 == next(it)
assert 1 == next(it)
示例4: test_safe_first_element_pandas_series
# 需要導入模塊: from matplotlib import cbook [as 別名]
# 或者: from matplotlib.cbook import safe_first_element [as 別名]
def test_safe_first_element_pandas_series(pd):
# delibrately create a pandas series with index not starting from 0
s = pd.Series(range(5), index=range(10, 15))
actual = cbook.safe_first_element(s)
assert actual == 0
示例5: get_converter
# 需要導入模塊: from matplotlib import cbook [as 別名]
# 或者: from matplotlib.cbook import safe_first_element [as 別名]
def get_converter(self, x):
"""
Get the converter for data that has the same type as *x*. If no
converters are registered for *x*, returns ``None``.
"""
if not len(self):
return None # nothing registered
# DISABLED idx = id(x)
# DISABLED cached = self._cached.get(idx)
# DISABLED if cached is not None: return cached
converter = None
classx = getattr(x, '__class__', None)
if classx is not None:
converter = self.get(classx)
if converter is None and hasattr(x, "values"):
# this unpacks pandas series or dataframes...
x = x.values
# If x is an array, look inside the array for data with units
if isinstance(x, np.ndarray) and x.size:
xravel = x.ravel()
try:
# pass the first value of x that is not masked back to
# get_converter
if not np.all(xravel.mask):
# some elements are not masked
converter = self.get_converter(
xravel[np.argmin(xravel.mask)])
return converter
except AttributeError:
# not a masked_array
# Make sure we don't recurse forever -- it's possible for
# ndarray subclasses to continue to return subclasses and
# not ever return a non-subclass for a single element.
next_item = xravel[0]
if (not isinstance(next_item, np.ndarray) or
next_item.shape != x.shape):
converter = self.get_converter(next_item)
return converter
# If we haven't found a converter yet, try to get the first element
if converter is None:
try:
thisx = safe_first_element(x)
except (TypeError, StopIteration):
pass
else:
if classx and classx != getattr(thisx, '__class__', None):
converter = self.get_converter(thisx)
return converter
# DISABLED self._cached[idx] = converter
return converter