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

MEP12 on set_and_get.py #4684

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 2 commits into from
Jul 15, 2015
Merged
Changes from 1 commit
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
Next Next commit
MEP12 on set_and_get.py
  • Loading branch information
ericmjl committed Jul 14, 2015
commit 982cecd6109867c1b318e71ece32b9eae97ddc74
58 changes: 30 additions & 28 deletions 58 examples/pylab_examples/set_and_get.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
"""

MATLAB and pylab allow you to use setp and get to set and get
MATLAB and pyplot allow you to use setp and get to set and get
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go farther here: I don't think we need to refer to Matlab at all, and although "get" is an alias for "getp", I think it would be better to encourage use of the symmetric pair, setp and getp. These are indeed what is used in the example.

object properties, as well as to do introspection on the object

set
To set the linestyle of a line to be dashed, you can do

>>> line, = plot([1,2,3])
>>> setp(line, linestyle='--')
>>> line, = plt.plot([1,2,3])
>>> plt.setp(line, linestyle='--')

If you want to know the valid types of arguments, you can provide the
name of the property you want to set without a value

>>> setp(line, 'linestyle')
>>> plt.setp(line, 'linestyle')
linestyle: [ '-' | '--' | '-.' | ':' | 'steps' | 'None' ]

If you want to see all the properties that can be set, and their
possible values, you can do

>>> setp(line)
>>> plt.setp(line)

set operates on a single instance or a list of instances. If you are
in query mode introspecting the possible values, only the first
instance in the sequence is used. When actually setting values, all
the instances will be set. e.g., suppose you have a list of two lines,
the following will make both lines thicker and red

>>> x = arange(0,1.0,0.01)
>>> y1 = sin(2*pi*x)
>>> y2 = sin(4*pi*x)
>>> lines = plot(x, y1, x, y2)
>>> setp(lines, linewidth=2, color='r')
>>> x = np.arange(0,1.0,0.01)
>>> y1 = np.sin(2*np.pi*x)
>>> y2 = np.sin(4*np.pi*x)
>>> lines = plt.plot(x, y1, x, y2)
>>> plt.setp(lines, linewidth=2, color='r')


get:

get returns the value of a given attribute. You can use get to query
the value of a single attribute

>>> getp(line, 'linewidth')
>>> plt.getp(line, 'linewidth')
0.5

or all the attribute/value pairs

>>> getp(line)
>>> plt.getp(line)
aa = True
alpha = 1.0
antialiased = True
Expand All @@ -65,33 +65,35 @@
"""

from __future__ import print_function
from pylab import *
# from pylab import *
import matplotlib.pyplot as plt
import numpy as np


x = arange(0, 1.0, 0.01)
y1 = sin(2*pi*x)
y2 = sin(4*pi*x)
lines = plot(x, y1, x, y2)
x = np.arange(0, 1.0, 0.01)
y1 = np.sin(2*np.pi*x)
y2 = np.sin(4*np.pi*x)
lines = plt.plot(x, y1, x, y2)
l1, l2 = lines
setp(lines, linestyle='--') # set both to dashed
setp(l1, linewidth=2, color='r') # line1 is thick and red
setp(l2, linewidth=1, color='g') # line2 is thicker and green
plt.setp(lines, linestyle='--') # set both to dashed
plt.setp(l1, linewidth=2, color='r') # line1 is thick and red
plt.setp(l2, linewidth=1, color='g') # line2 is thicker and green


print('Line setters')
setp(l1)
plt.setp(l1)
print('Line getters')
getp(l1)
plt.getp(l1)

print('Rectangle setters')
setp(gca().patch)
plt.setp(plt.gca().patch)
print('Rectangle getters')
getp(gca().patch)
plt.getp(plt.gca().patch)

t = title('Hi mom')
t = plt.title('Hi mom')
print('Text setters')
setp(t)
plt.setp(t)
print('Text getters')
getp(t)
plt.getp(t)

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