codex/past-exam-of-the-physics-2-paper-of-the-university-of-oxford/2021-circuit-arrangements.py

past-exam-of-the-physics-2-paper-of-the-university-of-oxford/2021-circuit-arrangements.py
"""Original circuit schematics; Python 3.14, numpy 2.3.5, matplotlib 3.10.7."""
from pathlib import Path
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, Polygon, Arc

plt.rcParams.update({'font.size': 12, 'font.family': 'DejaVu Sans'})
fig, axes = plt.subplots(2, 1, figsize=(9, 5.5), facecolor='white', gridspec_kw={'height_ratios': [1, 1.25]})

def wire(ax, points, **kw):
    ax.plot(*zip(*points), color='#26364a', lw=1.7, **kw)

def resistor(ax, x, y, label, w=1.1, h=.3):
    ax.add_patch(Rectangle((x-w/2, y-h/2), w, h, fill=False, ec='#26364a', lw=1.7))
    ax.text(x, y+.25, label, ha='center', va='bottom')

def capacitor(ax, x, y, label):
    wire(ax, [(x-.12, y-.32), (x-.12, y+.32)])
    wire(ax, [(x+.12, y-.32), (x+.12, y+.32)])
    ax.text(x, y+.4, label, ha='center')

def ground(ax, x, y):
    wire(ax, [(x, y+.25), (x, y)])
    for i, half in enumerate([.23, .15, .07]):
        wire(ax, [(x-half,y-.09*i), (x+half,y-.09*i)])

def dot(ax, x, y):
    ax.plot(x,y,'o',color='#26364a',ms=4)

for ax in axes:
    ax.set_xlim(0, 12)
    ax.axis('off')

a=axes[0];a.set_ylim(-.3,2.6)
a.set_title('(a) Parallel RC black box',loc='left',fontweight='bold')
wire(a,[(1,1),(2,1),(2,2),(5.45,2)]);resistor(a,6,2,'R');wire(a,[(6.55,2),(10,2),(10,1),(11,1)])
wire(a,[(2,1),(2,0),(5.88,0)]);capacitor(a,6,0,'C');wire(a,[(6.12,0),(10,0),(10,1)])
a.text(.6,1,'+',ha='center');a.text(11.4,1,'−',ha='center');a.text(6,1,'Input terminals',ha='center',color='#4e647c')

a=axes[1];a.set_ylim(-.55,3.3)
a.set_title('(b) Series resistor feeding a parallel LC branch',loc='left',fontweight='bold')
wire(a,[(.8,2.3),(2.45,2.3)]);resistor(a,3,2.3,'R');wire(a,[(3.55,2.3),(10.5,2.3)])
wire(a,[(.8,-.15),(10.5,-.15)])
wire(a,[(6,2.3),(6,1.25)]);wire(a,[(5.65,1.25),(6.35,1.25)]);wire(a,[(5.65,1.03),(6.35,1.03)]);wire(a,[(6,1.03),(6,-.15)])
a.text(6.5,1.1,'C',va='center')
wire(a,[(9,2.3),(9,1.95)])
a.add_patch(Rectangle((8.82,1.35),.36,.6,fill=False,ec='#26364a',lw=1.7));a.text(9.5,1.65,r'$R_L$',va='center')
wire(a,[(9,1.35),(9,1.12)])
for cy in [.98,.72,.46]:
    a.add_patch(Arc((9,cy),.52,.28,theta1=-90,theta2=90,color='#26364a',lw=1.7))
wire(a,[(9,.32),(9,-.15)]);a.text(9.5,.72,'L',va='center')
for x in [6,9]:dot(a,x,2.3);dot(a,x,-.15)
a.annotate('',(.8,2.2),(.8,-.05),arrowprops={'arrowstyle':'->','color':'#3564a0'});a.text(.35,1,r'$U_1$',ha='center')
a.annotate('',(10.5,2.2),(10.5,-.05),arrowprops={'arrowstyle':'->','color':'#3564a0'});a.text(11,1,r'$U_2$',ha='center')

fig.tight_layout(h_pad=2)
fig.savefig(Path.cwd()/Path(__file__).with_suffix('.png').name,dpi=120,facecolor='white',transparent=False)