|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import numpy as np |
| 3 | + |
| 4 | + |
| 5 | +# Functions for example plot |
| 6 | +def db(x): |
| 7 | + return 20.0 * np.log10(x) |
| 8 | + |
| 9 | + |
| 10 | +def model(f): |
| 11 | + return 10.0 / (f - 1.0j * 10) |
| 12 | + |
| 13 | + |
| 14 | +# Plot with physics_plot style |
| 15 | +with plt.style.context("physics_plot.pp_base"): |
| 16 | + fig, (ax_m, ax_p) = plt.subplots( |
| 17 | + nrows=2, ncols=1, figsize=(5, 3.5), constrained_layout=True |
| 18 | + ) |
| 19 | + fig.suptitle(r"Low Pass Filter ($\texttt{physics_plot}$ style)") |
| 20 | + f = np.logspace(0, 3, 1000) |
| 21 | + |
| 22 | + # Magnitude |
| 23 | + ax_m.semilogx(f, db(np.abs(model(f))), "r", label="LPF") |
| 24 | + ax_m.set_xlim(1e0, 1e3) |
| 25 | + ax_m.set_ylabel("Magnitude [dB]") |
| 26 | + ax_m.grid(visible=True, which="major") |
| 27 | + ax_m.grid(visible=True, which="minor", linestyle=":") |
| 28 | + ax_m.legend(loc=1) |
| 29 | + |
| 30 | + # Phase |
| 31 | + ax_p.semilogx(f, np.angle(model(f), deg=True), "r", label="LPF") |
| 32 | + ax_p.set_xlim(1e0, 1e3) |
| 33 | + ax_p.set_ylim(0, 90) |
| 34 | + ax_p.set_xlabel("Frequency [Hz]") |
| 35 | + ax_p.set_ylabel("Phase [deg]") |
| 36 | + ax_p.grid(visible=True, which="major") |
| 37 | + ax_p.grid(visible=True, which="minor", linestyle=":") |
| 38 | + ax_p.legend(loc=1) |
| 39 | + |
| 40 | + # fig.savefig("with-pp@2x.png") |
| 41 | + fig.savefig("bode-plot@2x.png") |
| 42 | + |
| 43 | + |
| 44 | +# # Plot without physics_plot style |
| 45 | +# fig, (ax_m, ax_p) = plt.subplots( |
| 46 | +# nrows=2, ncols=1, figsize=(5, 3.5), constrained_layout=True |
| 47 | +# ) |
| 48 | +# fig.suptitle("Low Pass Filter (Matplotlib default style)") |
| 49 | +# f = np.logspace(0, 3, 1000) |
| 50 | + |
| 51 | +# # Magnitude |
| 52 | +# ax_m.semilogx(f, db(np.abs(model(f))), "b", label="LPF") |
| 53 | +# ax_m.set_xlim(1e0, 1e3) |
| 54 | +# ax_m.set_ylabel("Magnitude [dB]") |
| 55 | +# ax_m.grid(visible=True, which="major") |
| 56 | +# ax_m.grid(visible=True, which="minor", linestyle=":") |
| 57 | +# ax_m.legend(loc=1) |
| 58 | + |
| 59 | +# # Phase |
| 60 | +# ax_p.semilogx(f, np.angle(model(f), deg=True), "b", label="LPF") |
| 61 | +# ax_p.set_xlim(1e0, 1e3) |
| 62 | +# ax_p.set_ylim(0, 90) |
| 63 | +# ax_p.set_xlabel("Frequency [Hz]") |
| 64 | +# ax_p.set_ylabel("Phase [deg]") |
| 65 | +# ax_p.grid(visible=True, which="major") |
| 66 | +# ax_p.grid(visible=True, which="minor", linestyle=":") |
| 67 | +# ax_p.legend(loc=1) |
| 68 | + |
| 69 | +# fig.savefig("without-pp@2x.png", dpi=600) |
0 commit comments