Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH? BUG? pyplot.setp/Artist.setp does not accept non-indexable iterables of handles. #6212

Closed
@madphysicist

Description

@madphysicist

Setup

matplotlib.__version__ = 1.5.1
matplotlib.__version__numpy__ = 1.6
Python 3.5.1, IPython 4.1.2 running under anaconda
OS: Red Hat 6.5

Matplotlib installed with conda install matplotlib.

Issue

Supplying an iterable that is not a list or a tuple (or other indexable sequence) causes an issue with setp. This is probably OK with Python 2, but it is counter-intuitive for Python 3. I would expect the following to work, but it does not:

>>> import matplotlib.pyplot as plt
>>> import itertools
>>> lines1 = plt.plot(range(3), range(3), range(5), range(5))
>>> lines2 = plt.plot(range(4), range(4), range(6), range(6))
>>> plt.setp(itertools.chain(lines1, lines2), color='red')
Traceback (most recent call last):

  File "<ipython-input-6-2f274dd0d4c1>", line 1, in <module>
    plt.setp(itertools.chain(lines1, lines2), color='red')

  File "/home/jfoxrabi/miniconda3/lib/python3.5/site-packages/matplotlib/pyplot.py", line 351, in setp
    return _setp(*args, **kwargs)

  File "/home/jfoxrabi/miniconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 1437, in setp
    insp = ArtistInspector(obj)

  File "/home/jfoxrabi/miniconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 1032, in __init__
    if cbook.iterable(o) and len(o):

TypeError: object of type 'itertools.chain' has no len()

The expected behavior would be the same as for

>>> plt.setp(lines1 + lines2, color='red')

This example is a bit contrived to illustrate the issue, but I have a case where I am doing stuff to a bunch of lines from different places, then trying to set a couple of properties on them. Wrapping the iterator I have in list() is not a huge problem, but it is unexpected and probably should not be needed.

The docs are trivial to update in this case (I have already started a PR), but I will need to trace through a couple of things before I am sure I am sticking next(iter(x)) instead of x[0] in the right place.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions