|
1 | 1 | """ |
2 | 2 |
|
3 | | -MATLAB and pylab allow you to use setp and get to set and get |
| 3 | +The pyplot interface allows you to use setp and getp to set and get |
4 | 4 | object properties, as well as to do introspection on the object |
5 | 5 |
|
6 | | -set |
| 6 | +set: |
7 | 7 | To set the linestyle of a line to be dashed, you can do |
8 | 8 |
|
9 | | - >>> line, = plot([1,2,3]) |
10 | | - >>> setp(line, linestyle='--') |
| 9 | + >>> line, = plt.plot([1,2,3]) |
| 10 | + >>> plt.setp(line, linestyle='--') |
11 | 11 |
|
12 | 12 | If you want to know the valid types of arguments, you can provide the |
13 | 13 | name of the property you want to set without a value |
14 | 14 |
|
15 | | - >>> setp(line, 'linestyle') |
| 15 | + >>> plt.setp(line, 'linestyle') |
16 | 16 | linestyle: [ '-' | '--' | '-.' | ':' | 'steps' | 'None' ] |
17 | 17 |
|
18 | 18 | If you want to see all the properties that can be set, and their |
19 | 19 | possible values, you can do |
20 | 20 |
|
21 | | - >>> setp(line) |
| 21 | + >>> plt.setp(line) |
22 | 22 |
|
23 | 23 | set operates on a single instance or a list of instances. If you are |
24 | 24 | in query mode introspecting the possible values, only the first |
25 | 25 | instance in the sequence is used. When actually setting values, all |
26 | 26 | the instances will be set. e.g., suppose you have a list of two lines, |
27 | 27 | the following will make both lines thicker and red |
28 | 28 |
|
29 | | - >>> x = arange(0,1.0,0.01) |
30 | | - >>> y1 = sin(2*pi*x) |
31 | | - >>> y2 = sin(4*pi*x) |
32 | | - >>> lines = plot(x, y1, x, y2) |
33 | | - >>> setp(lines, linewidth=2, color='r') |
| 29 | + >>> x = np.arange(0,1.0,0.01) |
| 30 | + >>> y1 = np.sin(2*np.pi*x) |
| 31 | + >>> y2 = np.sin(4*np.pi*x) |
| 32 | + >>> lines = plt.plot(x, y1, x, y2) |
| 33 | + >>> plt.setp(lines, linewidth=2, color='r') |
34 | 34 |
|
35 | 35 |
|
36 | 36 | get: |
37 | 37 |
|
38 | 38 | get returns the value of a given attribute. You can use get to query |
39 | 39 | the value of a single attribute |
40 | 40 |
|
41 | | - >>> getp(line, 'linewidth') |
| 41 | + >>> plt.getp(line, 'linewidth') |
42 | 42 | 0.5 |
43 | 43 |
|
44 | 44 | or all the attribute/value pairs |
45 | 45 |
|
46 | | - >>> getp(line) |
| 46 | + >>> plt.getp(line) |
47 | 47 | aa = True |
48 | 48 | alpha = 1.0 |
49 | 49 | antialiased = True |
|
65 | 65 | """ |
66 | 66 |
|
67 | 67 | from __future__ import print_function |
68 | | -from pylab import * |
69 | 68 |
|
| 69 | +import matplotlib.pyplot as plt |
| 70 | +import numpy as np |
70 | 71 |
|
71 | | -x = arange(0, 1.0, 0.01) |
72 | | -y1 = sin(2*pi*x) |
73 | | -y2 = sin(4*pi*x) |
74 | | -lines = plot(x, y1, x, y2) |
| 72 | + |
| 73 | +x = np.arange(0, 1.0, 0.01) |
| 74 | +y1 = np.sin(2*np.pi*x) |
| 75 | +y2 = np.sin(4*np.pi*x) |
| 76 | +lines = plt.plot(x, y1, x, y2) |
75 | 77 | l1, l2 = lines |
76 | | -setp(lines, linestyle='--') # set both to dashed |
77 | | -setp(l1, linewidth=2, color='r') # line1 is thick and red |
78 | | -setp(l2, linewidth=1, color='g') # line2 is thicker and green |
| 78 | +plt.setp(lines, linestyle='--') # set both to dashed |
| 79 | +plt.setp(l1, linewidth=2, color='r') # line1 is thick and red |
| 80 | +plt.setp(l2, linewidth=1, color='g') # line2 is thicker and green |
79 | 81 |
|
80 | 82 |
|
81 | 83 | print('Line setters') |
82 | | -setp(l1) |
| 84 | +plt.setp(l1) |
83 | 85 | print('Line getters') |
84 | | -getp(l1) |
| 86 | +plt.getp(l1) |
85 | 87 |
|
86 | 88 | print('Rectangle setters') |
87 | | -setp(gca().patch) |
| 89 | +plt.setp(plt.gca().patch) |
88 | 90 | print('Rectangle getters') |
89 | | -getp(gca().patch) |
| 91 | +plt.getp(plt.gca().patch) |
90 | 92 |
|
91 | | -t = title('Hi mom') |
| 93 | +t = plt.title('Hi mom') |
92 | 94 | print('Text setters') |
93 | | -setp(t) |
| 95 | +plt.setp(t) |
94 | 96 | print('Text getters') |
95 | | -getp(t) |
| 97 | +plt.getp(t) |
96 | 98 |
|
97 | | -show() |
| 99 | +plt.show() |
0 commit comments