#!/usr/bin/python """Example of util.shift_scale_y() """ # pylint: disable=I0011, C0103, E1101, R0801 import numpy as np from modelicares import util from modelicares.texunit import number_label # Generate some random data. x = range(100) y = np.cumsum(np.random.random(100) - 0.5) y -= y.min() y *= 1e-3 y += 1e3 # Small magnitude and large offset ylabel = number_label('Velocity', 'mm/s') # Plot the data. ax = util.setup_subplots(2, 2, label='examples/shift_scale_y')[0] for a in ax: a.plot(x, y) a.set_ylabel(ylabel) # Shift and scale the axes. ax[0].set_title('Original plot') ax[1].set_title('After applying offset and factor') util.shift_scale_y(ax[1])
#!/usr/bin/python """Example of util.shift_scale_x() """ # pylint: disable=I0011, C0103, E1101 import numpy as np from modelicares import util from modelicares.texunit import number_label # Generate some random data. x = np.linspace(55478, 55486, 100) # Small range and large offset xlabel = number_label('Time', 's') y = np.cumsum(np.random.random(100) - 0.5) # Plot the data. ax = util.setup_subplots(2, 2)[0] for a in ax: a.plot(x, y) a.set_xlabel(xlabel) # Shift and scale the axes. ax[0].set_title('Original plot') ax[1].set_title('After applying offset and factor') util.shift_scale_x(ax[1])
def bode_plot(mag, phase, f, in_Hz=True, in_dB=True, in_deg=True, label=None, axes=None, *args, **kwargs): r"""Create a Bode plot for a system. **Arguments:** - *sys*: Linear input/output system (Lti) - *freqs*: List or frequencies or tuple of (min, max) frequencies over which to plot the system response. If *freqs* is *None*, then an appropriate range will be determined automatically. - *in_Hz*: If *True*, the frequencies (*freqs*) are in Hz and should be plotted in Hz (otherwise, rad/s) - *in_dB*: If *True*, plot the magnitude in dB - *in_deg*: If *True*, plot the phase in degrees (otherwise, radians) - *label*: Label for the legend, if added - *axes*: Tuple (pair) of axes to plot into If *None* or (*None*, None*), then axes are created - *\*args*, *\*\*kwargs*: Additional options to matplotlib (color, linestyle, etc.) **Returns:** 1. Axes of the magnitude and phase plots (tuple (pair) of matplotlib axes) **Example:** .. plot:: :include-source: >>> from control.matlab import ss >>> sys = ss("1. -2; 3. -4", "5.; 7", "6. 8", "9.") >>> axes = bode_plot(sys) """ phase = unwrap(phase) freq_unit = Hz if in_Hz else rad / s # Create axes if necessary. if axes is None or (None, None): axes = (plt.subplot(211), plt.subplot(212)) # Magnitude plot axes[0].semilogx(f / freq_unit, to_dB(mag) if in_dB else mag, label=label, *args, **kwargs) # Add a grid and labels. axes[0].grid(True) axes[0].grid(True, which='minor') axes[0].set_ylabel("Magnitude in dB" if in_dB else "Magnitude") # Phase plot axes[1].semilogx(f / freq_unit, phase / (deg if in_deg else rad), label=label, *args, **kwargs) # Add a grid and labels. axes[1].grid(True) axes[1].grid(True, which='minor') axes[1].set_xlabel(number_label("Frequency", "Hz" if in_Hz else "rad/s")) axes[1].set_ylabel(number_label("Phase", "deg" if in_deg else "rad")) return axes