codex/area-equivalent-lumen-shapes.py

area-equivalent-lumen-shapes.py
"""Illustrate equal-area lumen shapes; run in the desired PNG output directory.

Tested with the repository's Python/Matplotlib environment; uses only Matplotlib.
"""
from pathlib import Path
import math
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Ellipse


def main():
    fig, axes = plt.subplots(1, 2, figsize=(7.4, 3.8), dpi=100, facecolor="white")
    shapes = [Ellipse((0, 0), 4, 2), Circle((0, 0), math.sqrt(2))]
    labels = ["Elliptical lumen\nprincipal diameters 4 and 2 mm",
              "Area-equivalent circle\ndiameter 2√2 ≈ 2.828 mm"]
    for ax, shape, label in zip(axes, shapes, labels):
        ax.set_facecolor("white")
        ax.add_patch(Circle((0, 0), 2, fill=False, edgecolor="#8b949e",
                            linestyle="--", linewidth=1.5))
        shape.set_facecolor("#d4e8f5")
        shape.set_edgecolor("#175985")
        shape.set_linewidth(2)
        ax.add_patch(shape)
        ax.set(xlim=(-2.4, 2.4), ylim=(-2.4, 2.4), xlabel="x (mm)", ylabel="y (mm)")
        ax.set_aspect("equal")
        ax.set_xticks([-2, 0, 2])
        ax.set_yticks([-2, 0, 2])
        ax.tick_params(labelsize=9)
        ax.set_title(label, fontsize=10, pad=8)
        ax.grid(alpha=.15)
        for spine in ax.spines.values():
            spine.set_color("#c1c8ce")
    fig.suptitle("An ellipse and its area-equivalent circle", fontsize=13, y=.97)
    fig.text(.5, .025, "Both filled areas: 2π mm². Dashed reference: a 4 mm diameter circle.",
             ha="center", fontsize=10)
    fig.subplots_adjust(left=.08, right=.98, bottom=.18, top=.73, wspace=.40)
    fig.savefig(Path("area-equivalent-lumen-shapes.png"), facecolor="white", dpi=100)
    plt.close(fig)


if __name__ == "__main__":
    main()