@@ -135,6 +135,20 @@ def bode_plot(syslist, omega=None, dB=None, Hz=None, deg=None,
135135 else :
136136 omega = sp .logspace (np .log10 (omega_limits [0 ]), np .log10 (omega_limits [1 ]), endpoint = True )
137137
138+ if Plot :
139+ fig = plt .gcf ()
140+ ax_mag = None
141+ ax_phase = None
142+ for ax in fig .axes :
143+ if ax .get_label () == 'pycontrol-bode-mag' :
144+ ax_mag = ax
145+ elif ax .get_label () == 'pycontrol-bode-phs' :
146+ ax_phase = ax
147+ if ax_mag is None or ax_phase is None :
148+ plt .clf ()
149+ ax_mag = plt .subplot (211 , label = 'pycontrol-bode-mag' )
150+ ax_phase = plt .subplot (212 , label = 'pycontrol-bode-phs' , sharex = ax_mag )
151+
138152 mags , phases , omegas , nyquistfrqs = [], [], [], []
139153 for sys in syslist :
140154 if (sys .inputs > 1 or sys .outputs > 1 ):
@@ -177,8 +191,6 @@ def bode_plot(syslist, omega=None, dB=None, Hz=None, deg=None,
177191 figlabel = str (random .randint (1 , 1e6 ))
178192
179193 # Magnitude plot
180- ax_mag = plt .subplot (211 , label = figlabel );
181-
182194 if dB :
183195 pltline = ax_mag .semilogx (omega_plot , 20 * np .log10 (mag ),
184196 * args , ** kwargs )
@@ -194,7 +206,6 @@ def bode_plot(syslist, omega=None, dB=None, Hz=None, deg=None,
194206 ax_mag .set_ylabel ("Magnitude (dB)" if dB else "Magnitude" )
195207
196208 # Phase plot
197- ax_phase = plt .subplot (212 , sharex = ax_mag );
198209 if deg :
199210 phase_plot = phase * 180. / math .pi
200211 else :
@@ -362,32 +373,45 @@ def gangof4_plot(P, C, omega=None):
362373 S = feedback (1 , L );
363374 T = L * S ;
364375
365- # Create a unique label to fix bug in matplotlib<=2.1
366- # See https://github.com/matplotlib/matplotlib/issues/9024
367- import random
368- figlabel = str (random .randint (1 , 1e6 ))
376+ plot_axes = {'t' : None , 's' : None , 'ps' : None , 'cs' : None }
377+ for ax in plt .gcf ().axes :
378+ label = ax .get_label ()
379+ if label .startswith ('pycontrol-gof-' ):
380+ key = label [len ('pycontrol-gof-' ):]
381+ if key not in plot_axes :
382+ raise RuntimeError ("unknown gof axis type '{}'" .format (label ))
383+ plot_axes [key ] = ax
384+
385+ # if any are missing, start from scratch
386+ if any ((ax is None for ax in plot_axes .values ())):
387+ plt .clf ()
388+ plot_axes = {'t' : plt .subplot (221 ,label = 'pycontrol-gof-t' ),
389+ 'ps' : plt .subplot (222 ,label = 'pycontrol-gof-ps' ),
390+ 'cs' : plt .subplot (223 ,label = 'pycontrol-gof-cs' ),
391+ 's' : plt .subplot (224 ,label = 'pycontrol-gof-s' )}
392+
393+ # Plot the four sensitivity functions
369394
370- # Plot the four sensitivity functions
371395 #! TODO: Need to add in the mag = 1 lines
372396 mag_tmp , phase_tmp , omega = T .freqresp (omega );
373397 mag = np .squeeze (mag_tmp )
374398 phase = np .squeeze (phase_tmp )
375- plt . subplot ( 221 , label = figlabel ); plt .loglog (omega , mag );
399+ plot_axes [ 't' ] .loglog (omega , mag );
376400
377401 mag_tmp , phase_tmp , omega = (P * S ).freqresp (omega );
378402 mag = np .squeeze (mag_tmp )
379403 phase = np .squeeze (phase_tmp )
380- plt . subplot ( 222 , label = figlabel ); plt .loglog (omega , mag );
404+ plot_axes [ 'ps' ] .loglog (omega , mag );
381405
382406 mag_tmp , phase_tmp , omega = (C * S ).freqresp (omega );
383407 mag = np .squeeze (mag_tmp )
384408 phase = np .squeeze (phase_tmp )
385- plt . subplot ( 223 , label = figlabel ); plt .loglog (omega , mag );
409+ plot_axes [ 'cs' ] .loglog (omega , mag );
386410
387411 mag_tmp , phase_tmp , omega = S .freqresp (omega );
388412 mag = np .squeeze (mag_tmp )
389413 phase = np .squeeze (phase_tmp )
390- plt . subplot ( 224 , label = figlabel ); plt .loglog (omega , mag );
414+ plot_axes [ 's' ] .loglog (omega , mag );
391415
392416#
393417# Utility functions
0 commit comments