48
48
# Packages used by this module
49
49
from functools import partial
50
50
import numpy as np
51
- import matplotlib
51
+ import matplotlib as mpl
52
52
import matplotlib .pyplot as plt
53
53
from numpy import array , poly1d , row_stack , zeros_like , real , imag
54
54
import scipy .signal # signal processing toolbox
55
- import pylab # plotting routines
56
55
from .xferfcn import _convert_to_transfer_function
57
56
from .exception import ControlMIMONotImplemented
58
57
from .sisotool import _SisotoolUpdate
63
62
# Default values for module parameters
64
63
_rlocus_defaults = {
65
64
'rlocus.grid' : True ,
66
- 'rlocus.plotstr' : 'b' if int (matplotlib .__version__ [0 ]) == 1 else 'C0' ,
65
+ 'rlocus.plotstr' : 'b' if int (mpl .__version__ [0 ]) == 1 else 'C0' ,
67
66
'rlocus.print_gain' : True ,
68
67
'rlocus.plot' : True
69
68
}
70
69
71
70
72
71
# Main function: compute a root locus diagram
73
72
def root_locus (sys , kvect = None , xlim = None , ylim = None ,
74
- plotstr = None , plot = True , print_gain = None , grid = None , ** kwargs ):
73
+ plotstr = None , plot = True , print_gain = None , grid = None , ax = None ,
74
+ ** kwargs ):
75
75
76
76
"""Root locus plot
77
77
@@ -96,6 +96,8 @@ def root_locus(sys, kvect=None, xlim=None, ylim=None,
96
96
branches, calculate gain, damping and print.
97
97
grid : bool
98
98
If True plot omega-damping grid. Default is False.
99
+ ax : Matplotlib axis
100
+ axis on which to create root locus plot
99
101
100
102
Returns
101
103
-------
@@ -143,42 +145,33 @@ def root_locus(sys, kvect=None, xlim=None, ylim=None,
143
145
# Create the Plot
144
146
if plot :
145
147
if sisotool :
146
- f = kwargs ['fig' ]
147
- ax = f .axes [1 ]
148
-
148
+ fig = kwargs ['fig' ]
149
+ ax = fig .axes [1 ]
149
150
else :
150
- figure_number = pylab .get_fignums ()
151
- figure_title = [
152
- pylab .figure (numb ).canvas .get_window_title ()
153
- for numb in figure_number ]
154
- new_figure_name = "Root Locus"
155
- rloc_num = 1
156
- while new_figure_name in figure_title :
157
- new_figure_name = "Root Locus " + str (rloc_num )
158
- rloc_num += 1
159
- f = pylab .figure (new_figure_name )
160
- ax = pylab .axes ()
151
+ if ax is None :
152
+ ax = plt .gca ()
153
+ fig = ax .figure
154
+ ax .set_title ('Root Locus' )
161
155
162
156
if print_gain and not sisotool :
163
- f .canvas .mpl_connect (
157
+ fig .canvas .mpl_connect (
164
158
'button_release_event' ,
165
- partial (_RLClickDispatcher , sys = sys , fig = f ,
166
- ax_rlocus = f .axes [0 ], plotstr = plotstr ))
167
-
159
+ partial (_RLClickDispatcher , sys = sys , fig = fig ,
160
+ ax_rlocus = fig .axes [0 ], plotstr = plotstr ))
168
161
elif sisotool :
169
- f .axes [1 ].plot (
162
+ fig .axes [1 ].plot (
170
163
[root .real for root in start_mat ],
171
164
[root .imag for root in start_mat ],
172
165
'm.' , marker = 's' , markersize = 8 , zorder = 20 , label = 'gain_point' )
173
- f .suptitle (
166
+ fig .suptitle (
174
167
"Clicked at: %10.4g%+10.4gj gain: %10.4g damp: %10.4g" %
175
168
(start_mat [0 ][0 ].real , start_mat [0 ][0 ].imag ,
176
169
1 , - 1 * start_mat [0 ][0 ].real / abs (start_mat [0 ][0 ])),
177
- fontsize = 12 if int (matplotlib .__version__ [0 ]) == 1 else 10 )
178
- f .canvas .mpl_connect (
170
+ fontsize = 12 if int (mpl .__version__ [0 ]) == 1 else 10 )
171
+ fig .canvas .mpl_connect (
179
172
'button_release_event' ,
180
- partial (_RLClickDispatcher , sys = sys , fig = f ,
181
- ax_rlocus = f .axes [1 ], plotstr = plotstr ,
173
+ partial (_RLClickDispatcher , sys = sys , fig = fig ,
174
+ ax_rlocus = fig .axes [1 ], plotstr = plotstr ,
182
175
sisotool = sisotool ,
183
176
bode_plot_params = kwargs ['bode_plot_params' ],
184
177
tvect = kwargs ['tvect' ]))
@@ -580,7 +573,7 @@ def _RLFeedbackClicksPoint(event, sys, fig, ax_rlocus, sisotool=False):
580
573
fig .suptitle (
581
574
"Clicked at: %10.4g%+10.4gj gain: %10.4g damp: %10.4g" %
582
575
(s .real , s .imag , K .real , - 1 * s .real / abs (s )),
583
- fontsize = 12 if int (matplotlib .__version__ [0 ]) == 1 else 10 )
576
+ fontsize = 12 if int (mpl .__version__ [0 ]) == 1 else 10 )
584
577
585
578
# Remove the previous line
586
579
_removeLine (label = 'gain_point' , ax = ax_rlocus )
@@ -609,7 +602,7 @@ def _removeLine(label, ax):
609
602
610
603
def _sgrid_func (fig = None , zeta = None , wn = None ):
611
604
if fig is None :
612
- fig = pylab .gcf ()
605
+ fig = plt .gcf ()
613
606
ax = fig .gca ()
614
607
else :
615
608
ax = fig .axes [1 ]
0 commit comments