codex/homogeneity-order-confounding.py

homogeneity-order-confounding.py
"""Original linear-drift example; run from _media to write the PNG there."""
from pathlib import Path
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

times=np.arange(6)
values=100+times
colors=['#2463a6','#bf692d','#34835c']
fig,axes=plt.subplots(1,2,figsize=(8,3.5),dpi=120,layout='constrained')
fig.patch.set_facecolor('white')
fig.suptitle('Identical bottles can appear different when measured in order',fontsize=12)
for i,c in enumerate(colors):
 sl=slice(2*i,2*i+2)
 axes[0].scatter(times[sl],values[sl],color=c,label=f'Bottle {chr(65+i)}',s=45,zorder=3)
axes[0].plot(times,values,color='#666666',linestyle='--',linewidth=1,label='Instrument drift')
axes[0].set(xlabel='Measurement order',ylabel='Indication (chosen unit)',xticks=times)
axes[0].legend(fontsize=8,loc='upper left')
means=values.reshape(3,2).mean(axis=1)
axes[1].bar(['A','B','C'],means-100,bottom=100,color=colors,width=.55)
axes[1].axhline(100,color='#333333',linestyle=':',label='True common value')
axes[1].set(xlabel='Bottle',ylabel='Mean indication (chosen unit)',ylim=(99.5,106))
axes[1].legend(fontsize=8)
for ax in axes:
 ax.spines[['top','right']].set_visible(False)
 ax.grid(axis='y',alpha=.2)
 ax.set_axisbelow(True)
fig.savefig(Path('homogeneity-order-confounding.png'),facecolor='white')