[Matplotlib-users] Replacing deprecated use of pyplot.subplot
Rory Yorke
rory.yorke at gmail.com
Sat Jan 20 14:44:18 EST 2018
Hi,
I'm a contributor to the Python Control Systems Library [1], which uses
Matplotlib for plotting.
We recently noticed deprecation warnings due to how we use
pyplot.subplot. We use it in the Matlab manner of either getting a
handle to an existing axis, or creating one if no suitable axis exists.
The warning is
MatplotlibDeprecationWarning: Adding an axes using the same arguments
as a previous axes currently reuses the earlier instance. In a future
version, a new instance will always be created and returned.
Meanwhile, this warning can be suppressed, and the future behavior
ensured, by passing a unique label to each axes instance.
For example, to plot the frequency response a linear dynamical system
(AKA Bode plot of the system), the relevant function could be simplified
to:
def bode_plot(g):
freq, mag, phase = freq_resp(g)
subplot(211)
semilogx(freq, 20*log10(mag))
subplot(212)
semilogx(freq, phase)
We've replaced that with code like this:
def bode_plot(g):
freq, mag, phase = freq_resp(g)
ax_mag = None
ax_phase = None
for ax in gcf.axes():
if ax.get_label() == 'control-bode-magnitude':
ax_mag = ax
elif ax.get_label() == 'control-bode-phase':
ax_phase = ax
if ax_mag is None or ax_phase is None:
clf()
ax_mag = subplot(211, label = 'control-bode-magnitude')
ax_phase = subplot(212, label = 'control-bode-phase)
ax_mag.semilogx(freq, 20*log10(mag))
ax_phase.semilogx(freq, phase)
This means that calls like
bode_plot(g)
bode_plot(h)
will show the response of g and h on the same figure.
Is this method of using labels to check for existing axes reasonable?
Is there a better way?
Actual code exhibiting warnings at [2]; new code at [3]. The latter
link is to an as-yet unmerged branch, and may disappear.
Thanks,
Rory
[1] https://github.com/python-control/python-control
[2] https://github.com/python-control/python-control/blob/af8d4ee39dfa574c2b3b335f4cdb4be858ae469a/control/freqplot.py#L175
[3] https://github.com/murrayrm/python-control/blob/dc1820a4e64d73937c7de8df078c41ec1773e048/control/freqplot.py#L138
More information about the Matplotlib-users
mailing list