Skip to content

cbook.iterable -> np.iterable. #11959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ API deprecations
The following API elements are deprecated:

- ``tk_window_focus``,
- ``cbook.iterable``,
- ``mlab.demean``,
7 changes: 3 additions & 4 deletions examples/units/basic_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import matplotlib.units as units
import matplotlib.ticker as ticker
from matplotlib.cbook import iterable


class ProxyDelegate(object):
Expand Down Expand Up @@ -345,22 +344,22 @@ def axisinfo(unit, axis):
def convert(val, unit, axis):
if units.ConversionInterface.is_numlike(val):
return val
if iterable(val):
if np.iterable(val):
return [thisval.convert_to(unit).get_value() for thisval in val]
else:
return val.convert_to(unit).get_value()

@staticmethod
def default_units(x, axis):
'return the default unit for x or None'
if iterable(x):
if np.iterable(x):
for thisx in x:
return thisx.unit
return x.unit


def cos(x):
if iterable(x):
if np.iterable(x):
return [math.cos(val.convert_to(radians).get_value()) for val in x]
else:
return math.cos(x.convert_to(radians).get_value())
Expand Down
7 changes: 4 additions & 3 deletions examples/units/evans_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
to what kind of units client packages use.
"""

from matplotlib.cbook import iterable
import numpy as np

import matplotlib.units as units
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -49,15 +50,15 @@ def convert(obj, unit, axis):
if units.ConversionInterface.is_numlike(obj):
return obj

if iterable(obj):
if np.iterable(obj):
return [o.value(unit) for o in obj]
else:
return obj.value(unit)

@staticmethod
def default_units(x, axis):
'return the default unit for x or None'
if iterable(x):
if np.iterable(x):
for thisx in x:
return thisx.unit
else:
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,7 @@ def __init__(self, fig, func, frames=None, init_func=None, fargs=None,
self._iter_gen = itertools.count
elif callable(frames):
self._iter_gen = frames
elif cbook.iterable(frames):
elif np.iterable(frames):
self._iter_gen = lambda: iter(frames)
if hasattr(frames, '__len__'):
self.save_count = len(frames)
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ def __init__(self, o):
responsibility to make sure this is so.
"""
if not isinstance(o, Artist):
if cbook.iterable(o):
if np.iterable(o):
o = list(o)
if len(o):
o = o[0]
Expand Down
43 changes: 21 additions & 22 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import matplotlib.ticker as mticker
import matplotlib.transforms as mtransforms
import matplotlib.tri as mtri
from matplotlib.cbook import iterable
from matplotlib.container import BarContainer, ErrorbarContainer, StemContainer
from matplotlib.axes._base import _AxesBase, _process_plot_format

Expand Down Expand Up @@ -1035,11 +1034,11 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid',
xmin = self.convert_xunits(xmin)
xmax = self.convert_xunits(xmax)

if not iterable(y):
if not np.iterable(y):
y = [y]
if not iterable(xmin):
if not np.iterable(xmin):
xmin = [xmin]
if not iterable(xmax):
if not np.iterable(xmax):
xmax = [xmax]

y, xmin, xmax = cbook.delete_masked_points(y, xmin, xmax)
Expand Down Expand Up @@ -1113,11 +1112,11 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid',
ymin = self.convert_yunits(ymin)
ymax = self.convert_yunits(ymax)

if not iterable(x):
if not np.iterable(x):
x = [x]
if not iterable(ymin):
if not np.iterable(ymin):
ymin = [ymin]
if not iterable(ymax):
if not np.iterable(ymax):
ymax = [ymax]

x, ymin, ymax = cbook.delete_masked_points(x, ymin, ymax)
Expand Down Expand Up @@ -1243,9 +1242,9 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
lineoffsets = self.convert_yunits(lineoffsets)
linelengths = self.convert_yunits(linelengths)

if not iterable(positions):
if not np.iterable(positions):
positions = [positions]
elif any(iterable(position) for position in positions):
elif any(np.iterable(position) for position in positions):
positions = [np.asanyarray(position) for position in positions]
else:
positions = [np.asanyarray(positions)]
Expand All @@ -1259,15 +1258,15 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
linewidths = cbook.local_over_kwdict(linewidths, kwargs, 'linewidth')
linestyles = cbook.local_over_kwdict(linestyles, kwargs, 'linestyle')

if not iterable(lineoffsets):
if not np.iterable(lineoffsets):
lineoffsets = [lineoffsets]
if not iterable(linelengths):
if not np.iterable(linelengths):
linelengths = [linelengths]
if not iterable(linewidths):
if not np.iterable(linewidths):
linewidths = [linewidths]
if not iterable(colors):
if not np.iterable(colors):
colors = [colors]
if hasattr(linestyles, 'lower') or not iterable(linestyles):
if hasattr(linestyles, 'lower') or not np.iterable(linestyles):
linestyles = [linestyles]

lineoffsets = np.asarray(lineoffsets)
Expand Down Expand Up @@ -3065,18 +3064,18 @@ def errorbar(self, x, y, yerr=None, xerr=None,
ecolor = base_style['color']
# make sure all the args are iterable; use lists not arrays to
# preserve units
if not iterable(x):
if not np.iterable(x):
x = [x]

if not iterable(y):
if not np.iterable(y):
y = [y]

if xerr is not None:
if not iterable(xerr):
if not np.iterable(xerr):
xerr = [xerr] * len(x)

if yerr is not None:
if not iterable(yerr):
if not np.iterable(yerr):
yerr = [yerr] * len(y)

# make the style dict for the 'normal' plot line
Expand Down Expand Up @@ -3168,7 +3167,7 @@ def extract_err(err, data):
except (TypeError, ValueError):
pass
else:
if iterable(a) and iterable(b):
if np.iterable(a) and np.iterable(b):
# using list comps rather than arrays to preserve units
low = [thisx - thiserr for thisx, thiserr
in cbook.safezip(data, a)]
Expand Down Expand Up @@ -4437,7 +4436,7 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
x, y, C = cbook.delete_masked_points(x, y, C)

# Set the size of the hexagon grid
if iterable(gridsize):
if np.iterable(gridsize):
nx, ny = gridsize
else:
nx = gridsize
Expand Down Expand Up @@ -4632,7 +4631,7 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
norm.autoscale(accum)

if bins is not None:
if not iterable(bins):
if not np.iterable(bins):
minimum, maximum = min(accum), max(accum)
bins -= 1 # one less edge than bins
bins = minimum + (maximum - minimum) * np.arange(bins) / bins
Expand Down Expand Up @@ -6514,7 +6513,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
bin_range = self.convert_xunits(bin_range)

# Check whether bins or range are given explicitly.
binsgiven = (cbook.iterable(bins) or bin_range is not None)
binsgiven = np.iterable(bins) or bin_range is not None

# We need to do to 'weights' what was done to 'x'
if weights is not None:
Expand Down
10 changes: 5 additions & 5 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from matplotlib import cbook, rcParams
from matplotlib.cbook import (
_OrderedSet, _check_1d, _string_to_bool, iterable, index_of, get_label)
_OrderedSet, _check_1d, _string_to_bool, index_of, get_label)
from matplotlib import docstring
import matplotlib.colors as mcolors
import matplotlib.lines as mlines
Expand Down Expand Up @@ -3005,7 +3005,7 @@ def set_xbound(self, lower=None, upper=None):

.. ACCEPTS: (lower: float, upper: float)
"""
if upper is None and iterable(lower):
if upper is None and np.iterable(lower):
lower, upper = lower

old_lower, old_upper = self.get_xbound()
Expand Down Expand Up @@ -3124,7 +3124,7 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
>>> set_xlim(5000, 0)

"""
if right is None and iterable(left):
if right is None and np.iterable(left):
left, right = left
if xmin is not None:
cbook.warn_deprecated('3.0', name='`xmin`',
Expand Down Expand Up @@ -3357,7 +3357,7 @@ def set_ybound(self, lower=None, upper=None):

.. ACCEPTS: (lower: float, upper: float)
"""
if upper is None and iterable(lower):
if upper is None and np.iterable(lower):
lower, upper = lower

old_lower, old_upper = self.get_ybound()
Expand Down Expand Up @@ -3457,7 +3457,7 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,

>>> set_ylim(5000, 0)
"""
if top is None and iterable(bottom):
if top is None and np.iterable(bottom):
bottom, top = bottom
if ymin is not None:
cbook.warn_deprecated('3.0', name='`ymin`',
Expand Down
5 changes: 3 additions & 2 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ class is even handier, and prettier to use. Whenever you want to
pass


@deprecated('3.1', alternative='np.iterable')
def iterable(obj):
"""return true if *obj* is iterable"""
try:
Expand Down Expand Up @@ -415,7 +416,7 @@ def open_file_cm(path_or_file, mode="r", encoding=None):

def is_scalar_or_string(val):
"""Return whether the given object is a scalar or string like."""
return isinstance(val, str) or not iterable(val)
return isinstance(val, str) or not np.iterable(val)


def _string_to_bool(s):
Expand Down Expand Up @@ -1026,7 +1027,7 @@ def delete_masked_points(*args):
margs = []
seqlist = [False] * len(args)
for i, x in enumerate(args):
if not isinstance(x, str) and iterable(x) and len(x) == nrecs:
if not isinstance(x, str) and np.iterable(x) and len(x) == nrecs:
seqlist[i] = True
if isinstance(x, np.ma.MaskedArray):
if x.ndim > 1:
Expand Down
10 changes: 5 additions & 5 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def __init__(self, ax, cmap=None,
self.ticklocation = ticklocation

self.set_label(label)
if cbook.iterable(ticks):
if np.iterable(ticks):
self.locator = ticker.FixedLocator(ticks, nbins=len(ticks))
else:
self.locator = ticks # Handle default in _ticker()
Expand Down Expand Up @@ -559,7 +559,7 @@ def set_ticks(self, ticks, update_ticks=True):
use :meth:`update_ticks` to manually update the ticks.

"""
if cbook.iterable(ticks):
if np.iterable(ticks):
self.locator = ticker.FixedLocator(ticks, nbins=len(ticks))
else:
self.locator = ticks
Expand Down Expand Up @@ -707,9 +707,9 @@ def add_lines(self, levels, colors, linewidths, erase=True):
y = self._locate(levels)
igood = (y < 1.001) & (y > -0.001)
y = y[igood]
if cbook.iterable(colors):
if np.iterable(colors):
colors = np.asarray(colors)[igood]
if cbook.iterable(linewidths):
if np.iterable(linewidths):
linewidths = np.asarray(linewidths)[igood]
X, Y = np.meshgrid([0, 1], y)
if self.orientation == 'vertical':
Expand Down Expand Up @@ -1289,7 +1289,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
anchor = kw.pop('anchor', loc_settings['anchor'])
parent_anchor = kw.pop('panchor', loc_settings['panchor'])

parents_iterable = cbook.iterable(parents)
parents_iterable = np.iterable(parents)
# turn parents into a list if it is not already. We do this w/ np
# because `plt.subplots` can return an ndarray and is natural to
# pass to `colorbar`.
Expand Down
14 changes: 7 additions & 7 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ def __call__(self, X, alpha=None, bytes=False):
if not self._isinit:
self._init()
mask_bad = None
if not cbook.iterable(X):
if not np.iterable(X):
vtype = 'scalar'
xa = np.array([X])
else:
Expand Down Expand Up @@ -716,7 +716,7 @@ def from_list(name, colors, N=256, gamma=1.0):
to divide the range unevenly.
"""

if not cbook.iterable(colors):
if not np.iterable(colors):
raise ValueError('colors must be iterable')

if (isinstance(colors[0], Sized) and len(colors[0]) == 2
Expand Down Expand Up @@ -812,7 +812,7 @@ def __init__(self, colors, name='from_list', N=None):
if isinstance(colors, str):
self.colors = [colors] * N
self.monochrome = True
elif cbook.iterable(colors):
elif np.iterable(colors):
if len(colors) == 1:
self.monochrome = True
self.colors = list(
Expand Down Expand Up @@ -907,7 +907,7 @@ def process_value(value):
Experimental; we may want to add an option to force the
use of float32.
"""
is_scalar = not cbook.iterable(value)
is_scalar = not np.iterable(value)
if is_scalar:
value = [value]
dtype = np.min_scalar_type(value)
Expand Down Expand Up @@ -962,7 +962,7 @@ def inverse(self, value):
(vmin,), _ = self.process_value(self.vmin)
(vmax,), _ = self.process_value(self.vmax)

if cbook.iterable(value):
if np.iterable(value):
val = np.ma.asarray(value)
return vmin + val * (vmax - vmin)
else:
Expand Down Expand Up @@ -1035,7 +1035,7 @@ def inverse(self, value):
raise ValueError("Not invertible until scaled")
vmin, vmax = self.vmin, self.vmax

if cbook.iterable(value):
if np.iterable(value):
val = np.ma.asarray(value)
return vmin * np.ma.power((vmax / vmin), val)
else:
Expand Down Expand Up @@ -1222,7 +1222,7 @@ def inverse(self, value):
gamma = self.gamma
vmin, vmax = self.vmin, self.vmax

if cbook.iterable(value):
if np.iterable(value):
val = np.ma.asarray(value)
return np.ma.power(val, 1. / gamma) * (vmax - vmin) + vmin
else:
Expand Down
Loading