dsp/lib/plot.py

70 lines
2.0 KiB
Python
Raw Normal View History

2015-10-18 23:06:39 -07:00
import matplotlib.pyplot as plt
from matplotlib import ticker
2017-09-21 04:04:22 -07:00
2015-10-18 23:06:39 -07:00
def response_setup(ax, ymin=-24, ymax=24, yL=ticker.AutoMinorLocator(3)):
ax.set_xlim(20, 20000)
ax.set_ylim(ymin, ymax)
ax.set_yticks(tuple(range(ymin, ymax + 1, 6)))
ax.yaxis.set_minor_locator(yL)
ax.grid(True, 'both')
ax.set_xlabel('frequency (Hz)')
ax.set_ylabel('magnitude (dB)')
2017-09-21 04:04:22 -07:00
2015-10-19 05:39:37 -07:00
def phase_response_setup(ax, div=12, yL=ticker.AutoMinorLocator(2)):
ax.set_xlim(20, 20000)
ax.set_ylim(-180, 180)
ax.set_yticks(tuple(range(-180, 180 + 1, int(360/div))))
ax.yaxis.set_minor_locator(yL)
ax.grid(True, 'both')
ax.set_xlabel('frequency (Hz)')
ax.set_ylabel('phase (degrees)')
2017-09-21 04:04:22 -07:00
2015-10-18 23:06:39 -07:00
def cleanplot():
fig, ax = plt.subplots()
ax.set_axis_off()
2017-09-21 04:04:22 -07:00
ax.set_position([0, 0, 1, 1])
2015-10-18 23:06:39 -07:00
return fig, ax
2017-09-21 04:04:22 -07:00
2015-10-18 23:06:39 -07:00
def new_response(*args, **kwargs):
2017-03-22 04:04:21 -07:00
fig, ax = plt.subplots()
2015-10-18 23:06:39 -07:00
response_setup(ax, *args, **kwargs)
return fig, ax
2015-10-19 05:39:37 -07:00
2017-09-21 04:04:22 -07:00
2015-10-19 05:39:37 -07:00
def new_phase_response(*args, **kwargs):
2017-03-22 04:04:21 -07:00
fig, ax = plt.subplots()
2015-10-19 05:39:37 -07:00
phase_response_setup(ax, *args, **kwargs)
return fig, ax
2017-09-21 04:04:22 -07:00
2015-10-19 05:39:37 -07:00
def new_bode(magnitude_offset=0):
2017-03-22 04:04:21 -07:00
fig, ax1 = plt.subplots()
2015-10-19 05:39:37 -07:00
ax2 = ax1.twinx()
ymin = -24 + magnitude_offset
ymax = 24 + magnitude_offset
response_setup(ax1, ymin, ymax)
phase_response_setup(ax2)
2015-11-04 04:04:42 -08:00
2016-08-02 04:04:45 -07:00
cc = plt.style.library['ggplot']['axes.prop_cycle'].by_key()['color']
2015-11-04 04:04:42 -08:00
ax1.set_ylabel(ax1.get_ylabel(), color=cc[0])
ax2.set_ylabel(ax2.get_ylabel(), color=cc[1])
for tl in ax1.get_yticklabels():
tl.set_color(cc[0])
for tl in ax2.get_yticklabels():
tl.set_color(cc[1])
2017-09-21 04:04:22 -07:00
# ax1.hlines(0, 20, 40, linewidth=0.5, color=cc[0])
# ax2.hlines(0, 10000, 20000, linewidth=0.5, color=cc[1])
2015-11-04 04:04:42 -08:00
2016-08-02 04:04:45 -07:00
# share color cycles to prevent color re-use
ax2._get_lines.prop_cycler = ax1._get_lines.prop_cycler
2015-11-04 04:04:42 -08:00
2015-10-19 05:39:37 -07:00
# ax1 and ax2 should have identical grids;
# disable ax2's so it doesn't overlap ax1's plot lines.
ax2.grid(False, which='both')
return fig, ax1, ax2