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

Skip to content

Commit d208471

Browse files
committed
Merge pull request #6217 from madphysicist/madphysicist-patch-1
FIX: Made `setp` accept arbitrary iterables
2 parents 4e725b8 + 8881680 commit d208471

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

lib/matplotlib/artist.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,16 +1138,19 @@ class ArtistInspector(object):
11381138
def __init__(self, o):
11391139
"""
11401140
Initialize the artist inspector with an
1141-
:class:`~matplotlib.artist.Artist` or sequence of :class:`Artists`.
1142-
If a sequence is used, we assume it is a homogeneous sequence (all
1141+
:class:`~matplotlib.artist.Artist` or iterable of :class:`Artists`.
1142+
If an iterable is used, we assume it is a homogeneous sequence (all
11431143
:class:`Artists` are of the same type) and it is your responsibility
11441144
to make sure this is so.
11451145
"""
1146-
if cbook.iterable(o) and len(o):
1147-
o = o[0]
1146+
if cbook.iterable(o):
1147+
# Wrapped in list instead of doing try-except around next(iter(o))
1148+
o = list(o)
1149+
if len(o):
1150+
o = o[0]
11481151

11491152
self.oorig = o
1150-
if not isinstance(o, type):
1153+
if not inspect.isclass(o):
11511154
o = type(o)
11521155
self.o = o
11531156

@@ -1506,8 +1509,8 @@ def setp(obj, *args, **kwargs):
15061509
>>> line, = plot([1,2,3])
15071510
>>> setp(line, linestyle='--')
15081511
1509-
If you want to know the valid types of arguments, you can provide the
1510-
name of the property you want to set without a value::
1512+
If you want to know the valid types of arguments, you can provide
1513+
the name of the property you want to set without a value::
15111514
15121515
>>> setp(line, 'linestyle')
15131516
linestyle: [ '-' | '--' | '-.' | ':' | 'steps' | 'None' ]
@@ -1518,12 +1521,12 @@ def setp(obj, *args, **kwargs):
15181521
>>> setp(line)
15191522
... long output listing omitted
15201523
1521-
:func:`setp` operates on a single instance or a list of instances.
1522-
If you are in query mode introspecting the possible values, only
1523-
the first instance in the sequence is used. When actually setting
1524-
values, all the instances will be set. e.g., suppose you have a
1525-
list of two lines, the following will make both lines thicker and
1526-
red::
1524+
:func:`setp` operates on a single instance or a iterable of
1525+
instances. If you are in query mode introspecting the possible
1526+
values, only the first instance in the sequence is used. When
1527+
actually setting values, all the instances will be set. e.g.,
1528+
suppose you have a list of two lines, the following will make both
1529+
lines thicker and red::
15271530
15281531
>>> x = arange(0,1.0,0.01)
15291532
>>> y1 = sin(2*pi*x)
@@ -1538,7 +1541,12 @@ def setp(obj, *args, **kwargs):
15381541
>>> setp(lines, linewidth=2, color='r') # python style
15391542
"""
15401543

1541-
insp = ArtistInspector(obj)
1544+
if not cbook.iterable(obj):
1545+
objs = [obj]
1546+
else:
1547+
objs = list(cbook.flatten(obj))
1548+
1549+
insp = ArtistInspector(objs[0])
15421550

15431551
if len(kwargs) == 0 and len(args) == 0:
15441552
print('\n'.join(insp.pprint_setters()))
@@ -1548,11 +1556,6 @@ def setp(obj, *args, **kwargs):
15481556
print(insp.pprint_setters(prop=args[0]))
15491557
return
15501558

1551-
if not cbook.iterable(obj):
1552-
objs = [obj]
1553-
else:
1554-
objs = list(cbook.flatten(obj))
1555-
15561559
if len(args) % 2:
15571560
raise ValueError('The set args must be string, value pairs')
15581561

lib/matplotlib/tests/test_artist.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from matplotlib.externals import six
55

66
import io
7-
7+
from itertools import chain
88
import numpy as np
99

1010
import matplotlib.pyplot as plt
@@ -13,6 +13,7 @@
1313
import matplotlib.path as mpath
1414
import matplotlib.transforms as mtrans
1515
import matplotlib.collections as mcollections
16+
import matplotlib.artist as martist
1617
import matplotlib as mpl
1718
from matplotlib.testing.decorators import image_comparison, cleanup
1819

@@ -208,6 +209,15 @@ def test_properties():
208209
assert len(w) == 0
209210

210211

212+
@cleanup
213+
def test_setp():
214+
fig, axes = plt.subplots()
215+
lines1 = axes.plot(range(3))
216+
lines2 = axes.plot(range(3))
217+
martist.setp(chain(lines1, lines2), 'lw', 5)
218+
plt.setp(axes.spines.values(), color='green')
219+
220+
211221
if __name__ == '__main__':
212222
import nose
213223
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)