Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0b56a6a

Browse filesBrowse files
committed
ENH: Added file keyword to setp to redirect output
1 parent d208471 commit 0b56a6a
Copy full SHA for 0b56a6a

File tree

Expand file treeCollapse file tree

3 files changed

+28
-5
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+28
-5
lines changed

‎doc/users/whats_new/setp_output.rst

Copy file name to clipboard
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
`Artist.setp` (and `pyplot.setp`) accept a `file` argument
2+
----------------------------------------------------------
3+
4+
The argument is keyword-only. It allows an output file other than
5+
`sys.stdout` to be specified. It works exactly like the `file` argument
6+
to `print`.
7+

‎lib/matplotlib/artist.py

Copy file name to clipboardExpand all lines: lib/matplotlib/artist.py
+15-5Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,12 @@ def setp(obj, *args, **kwargs):
15211521
>>> setp(line)
15221522
... long output listing omitted
15231523
1524+
You may specify another output file to `setp` if `sys.stdout` is not
1525+
acceptable for some reason using the `file` keyword-only argument::
1526+
1527+
>>> with fopen('output.log') as f:
1528+
>>> setp(line, file=f)
1529+
15241530
:func:`setp` operates on a single instance or a iterable of
15251531
instances. If you are in query mode introspecting the possible
15261532
values, only the first instance in the sequence is used. When
@@ -1548,12 +1554,16 @@ def setp(obj, *args, **kwargs):
15481554

15491555
insp = ArtistInspector(objs[0])
15501556

1551-
if len(kwargs) == 0 and len(args) == 0:
1552-
print('\n'.join(insp.pprint_setters()))
1553-
return
1557+
# file has to be popped before checking if kwargs is empty
1558+
printArgs = {}
1559+
if 'file' in kwargs:
1560+
printArgs['file'] = kwargs.pop('file')
15541561

1555-
if len(kwargs) == 0 and len(args) == 1:
1556-
print(insp.pprint_setters(prop=args[0]))
1562+
if not kwargs and len(args) < 2:
1563+
if args:
1564+
print(insp.pprint_setters(prop=args[0]), **printArgs)
1565+
else:
1566+
print('\n'.join(insp.pprint_setters()), **printArgs)
15571567
return
15581568

15591569
if len(args) % 2:

‎lib/matplotlib/tests/test_artist.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_artist.py
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,18 @@ def test_properties():
211211

212212
@cleanup
213213
def test_setp():
214+
# Check arbitrary iterables
214215
fig, axes = plt.subplots()
215216
lines1 = axes.plot(range(3))
216217
lines2 = axes.plot(range(3))
217218
martist.setp(chain(lines1, lines2), 'lw', 5)
218219
plt.setp(axes.spines.values(), color='green')
219220

221+
# Check `file` argument
222+
sio = io.StringIO()
223+
plt.setp(lines1, 'zorder', file=sio)
224+
assert sio.getvalue() == ' zorder: any number \n'
225+
220226

221227
if __name__ == '__main__':
222228
import nose

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.