forked from python-control/python-control
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_funcs_tests_manual.py
More file actions
143 lines (114 loc) · 3.04 KB
/
Copy pathplot_funcs_tests_manual.py
File metadata and controls
143 lines (114 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Tests for plot functions
from control import tf, c2d, pzmap, rlocus, bode, nyquist, nichols, sisotool
from control.grid import sgrid, zgrid
import numpy as np
import matplotlib.pyplot as plt
import os
plot_dir = 'temp_plots'
if not os.path.exists(plot_dir):
os.mkdir(plot_dir)
# Helper function for saving figures
def savefig(label, fname='{}_fig{}.pdf'):
fig_n = plt.gcf().number
plt.savefig(os.path.join(plot_dir, fname.format(label, fig_n)))
# First test zgrid and sgrid on their own
plt.figure()
sgrid()
plt.title('s-grid')
savefig('grids1')
plt.figure()
zgrid()
plt.title('z-grid')
savefig('grids1')
# Display and wait for user to close all figures
plt.show()
# Adding grids to an existing figure
fig = plt.figure(figsize=(4.5, 4))
sgrid(fig=fig)
plt.title('s-grid')
savefig('grids2')
fig = plt.figure(figsize=(4.5, 4))
zgrid(fig=fig)
plt.title('z-grid')
savefig('grids2')
fig, ax = plt.subplots(figsize=(4.5, 4))
zgrid(ax=ax) # only works with zgrid
plt.title('z-grid #2')
savefig('grids2')
# Display and wait for user to close all figures
plt.show()
# Create figure with subplots
fig = plt.figure()
sgrid(position=(2, 2, 1))
ax2 = fig.add_subplot(2, 2, 2)
zgrid(ax=ax2) # alternative for zgrids
#zgrid(position=(2, 2, 2))
sgrid(position=(2, 2, 3))
ax4 = fig.add_subplot(2, 2, 4)
zgrid(ax=ax4) # alternative for zgrids
plt.tight_layout()
savefig('grids_subplots')
# Display and wait for user to close all figures
plt.show()
# Define test systems
s = tf('s')
G1 = 2*(s+1)/(s*(s-2))
G2 = tf(1, [1, 8, 30, 56, 65])
Gz1 = c2d(G1, 0.2)
Gz2 = c2d(G2, 0.2)
# Test rlocus function
# Single default plots
plt.figure()
rlocus(G1)
savefig('rlocus1')
plt.figure()
rlocus(G2)
savefig('rlocus1')
plt.figure()
rlocus(Gz1)
savefig('rlocus1')
plt.figure()
rlocus(Gz2)
savefig('rlocus1')
# Display and wait for user to close all figures
plt.show()
# Single default plots - without grid
plt.figure(figsize=(4.5, 4))
rlocus(G1, grid=False)
savefig('rlocus2')
plt.figure(figsize=(4.5, 4))
rlocus(G2, grid=False)
savefig('rlocus2')
plt.figure(figsize=(4.5, 4))
rlocus(Gz1, grid=False)
savefig('rlocus2')
plt.figure(figsize=(4.5, 4))
rlocus(Gz2, grid=False)
savefig('rlocus2')
# Display and wait for user to close all figures
plt.show()
# Subplots - discrete time
fig, axes = plt.subplots(2, 2)
rlocus(Gz1, ax=axes[0, 0], title='Gz1')
rlocus(Gz2, ax=axes[0, 1], title='Gz1')
rlocus(Gz1, ax=axes[1, 0], grid=False, title='Gz1 - no grid')
rlocus(Gz2, ax=axes[1, 1], grid=False, title='Gz1 - no grid')
plt.tight_layout()
savefig('rlocus_subplots1')
# Subplots - sontinuous time, done differently due to sgrid
fig = plt.figure()
rlocus(G1, position=(2, 2, 1), title='G1')
rlocus(G2, position=(2, 2, 2), title='G2')
rlocus(G1, position=(2, 2, 3), grid=False, title='G1 - no grid')
rlocus(G2, position=(2, 2, 4), grid=False, title='G2 - no grid')
plt.tight_layout()
savefig('rlocus_subplots1')
# Display and wait for user to close all figures
plt.show()
# Check sisotool works (includes rlocus plot)
sisotool(G1)
savefig('sisotool')
plt.show()
sisotool(Gz1)
savefig('sisotool')
plt.show()