codexlung-chest-recoil-balance.py
lung-chest-recoil-balance.py
"""Original local passive pressure-volume model; no clinical reference ranges.
Python 3.14; root NumPy and Matplotlib dependencies.
Run in the output directory to write an opaque white PNG to cwd.
"""
from pathlib import Path
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
v=np.linspace(2,4.2,301)
pl=5*(v-1);pw=5*(v-4);pt=pl+pw
fig,ax=plt.subplots(figsize=(8,3.5),dpi=120)
fig.patch.set_facecolor('white');ax.set_facecolor('white')
ax.plot(pl,v,label='Lung: $P_L=5(V-1)$',color='#3c72a6',lw=2)
ax.plot(pw,v,label='Chest wall: $P_W=5(V-4)$',color='#b66032',lw=2)
ax.plot(pt,v,label='Total: $P_L+P_W$',color='#43876d',lw=2)
ax.axvline(0,color='#777777',ls=':',lw=1)
ax.axhline(2.5,color='#777777',ls='--',lw=.8)
ax.plot([7.5,-7.5,0],[2.5]*3,'o',color='#555555',ms=4)
ax.annotate('Equal and opposite recoil\nat volume 2.5 L',xy=(0,2.5),xytext=(1,2.12),
arrowprops={'arrowstyle':'->','color':'#555555'},fontsize=9)
ax.set(xlim=(-12,20),ylim=(1.95,4.25),xlabel='Elastic pressure (cm H₂O)',ylabel='Volume (L)',
title='Passive recoil balance: lung, chest wall and total')
ax.grid(alpha=.14);ax.legend(fontsize=8,loc='upper left')
fig.tight_layout();fig.savefig(Path.cwd()/'lung-chest-recoil-balance.png',facecolor='white');plt.close(fig)