import math
import numpy as np
def plot_re_mag(ax, dut_plot):
color = next(ax._get_lines.prop_cycler)['color']
ax.plot(dut_plot.f, np.where(dut_plot.z_re[:, 0, 0] < 0, np.nan, abs(dut_plot.z_re[:,0,0])), color=color, label=dut_plot.name)
ax.plot(dut_plot.f, np.where(dut_plot.z_re[:, 0, 0] > 0, np.nan, abs(dut_plot.z_re[:,0,0])), ':', color=color)
ax.set_ylim(0.01,100)
ax.set_yscale("log")
ax.set_title("Magnitude of real component of impedance")
ax.yaxis.set_major_formatter(lambda y,pos: f"{10 * math.log10(y):.0f} dB ohm")
ax.legend(loc="upper right", fontsize=6)
ax.xaxis.set_major_formatter(lambda y,pos: f"{y/1e9:.1f}\nGHz")
def plot_im_mag(ax, dut_plot):
color = next(ax._get_lines.prop_cycler)['color']
ax.plot(dut_plot.f, np.where(dut_plot.z_im[:, 0, 0] < 0, np.nan, abs(dut_plot.z_re[:,0,0])), color=color, label=dut_plot.name)
ax.plot(dut_plot.f, np.where(dut_plot.z_im[:, 0, 0] > 0, np.nan, abs(dut_plot.z_re[:,0,0])), ':', color=color)
ax.set_ylim(0.1,10000)
ax.set_yscale("log")
ax.set_title("Magnitude of imag component of impedance")
ax.yaxis.set_major_formatter(lambda y,pos: f"{10 * math.log10(y):.0f} dB ohm")
ax.legend(loc="upper right", fontsize=6)
ax.xaxis.set_major_formatter(lambda y,pos: f"{y/1e9:.1f}\nGHz")
fig,(ax0, ax1) = plt.subplots(2,1, sharex = True, figsize=(7,6))
plot_re_mag(ax0, dut_boardstd)
plot_im_mag(ax1, dut_boardstd)