|
| 1 | +""" |
| 2 | +Demo: automatic nice-number axis bounds in range_frame |
| 3 | +====================================================== |
| 4 | +
|
| 5 | +This script demonstrates the ``nice=True`` feature of ``range_frame``. |
| 6 | +With ``nice=True`` (the default), spine bounds snap to tick positions so |
| 7 | +that the axis line starts and ends exactly on round numbers. |
| 8 | +
|
| 9 | +The LEFT column uses nice=True → spines always land on tick marks. |
| 10 | +The RIGHT column uses nice=False → spines sit at raw data min/max |
| 11 | +(the old behaviour, shown for comparison — tick misalignment is expected). |
| 12 | +""" |
| 13 | + |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +import numpy as np |
| 16 | + |
| 17 | +from lama_aesthetics.aesthetics import get_style |
| 18 | +from lama_aesthetics.plotutils import range_frame |
| 19 | + |
| 20 | +# ── style ────────────────────────────────────────────────────────────────── |
| 21 | +get_style("main") |
| 22 | + |
| 23 | +# ── datasets ──────────────────────────────────────────────────────────────── |
| 24 | +# Each tuple: (title, x, y) |
| 25 | +DATASETS = [ |
| 26 | + ( |
| 27 | + "Integers 0–4 vs 0–8\n(common case)", |
| 28 | + np.array([0, 1, 2, 3, 4]), |
| 29 | + np.array([0, 2, 4, 6, 8]), |
| 30 | + ), |
| 31 | + ( |
| 32 | + "Messy floats\n(3.2 – 47.8)", |
| 33 | + np.linspace(3.2, 47.8, 12), |
| 34 | + np.linspace(3.2, 47.8, 12) * 1.3 - 1.5, |
| 35 | + ), |
| 36 | + ( |
| 37 | + "Sub-unit range\n(0.031 – 0.097)", |
| 38 | + np.linspace(0.031, 0.097, 10), |
| 39 | + np.linspace(0.031, 0.097, 10) ** 0.5, |
| 40 | + ), |
| 41 | + ( |
| 42 | + "Large numbers\n(12 400 – 98 700)", |
| 43 | + np.linspace(12_400, 98_700, 8), |
| 44 | + np.linspace(12_400, 98_700, 8) * 0.7 + 3_000, |
| 45 | + ), |
| 46 | + ( |
| 47 | + "Negative range\n(−73 – −12)", |
| 48 | + np.linspace(-73, -12, 10), |
| 49 | + np.linspace(-73, -12, 10) * -0.5 - 5, |
| 50 | + ), |
| 51 | +] |
| 52 | + |
| 53 | +# ── figure layout ──────────────────────────────────────────────────────────── |
| 54 | +n_rows = len(DATASETS) |
| 55 | +fig, axes = plt.subplots(n_rows, 2, figsize=(10, 2.8 * n_rows)) |
| 56 | + |
| 57 | +# Column headers |
| 58 | +axes[0, 0].set_title( |
| 59 | + "nice=True (NEW — spines snap to ticks)", |
| 60 | + fontsize=9, |
| 61 | + fontweight="bold", |
| 62 | + color="green", |
| 63 | + pad=8, |
| 64 | +) |
| 65 | +axes[0, 1].set_title( |
| 66 | + "nice=False (OLD — spines at raw data min/max)", |
| 67 | + fontsize=9, |
| 68 | + fontweight="bold", |
| 69 | + color="red", |
| 70 | + pad=8, |
| 71 | +) |
| 72 | + |
| 73 | +for row, (title, x, y) in enumerate(DATASETS): |
| 74 | + for col, nice in enumerate([True, False]): |
| 75 | + ax = axes[row, col] |
| 76 | + |
| 77 | + ax.plot(x, y, marker="o", markersize=4, linewidth=1.5, label=title) |
| 78 | + range_frame(ax, x, y, nice=nice) |
| 79 | + |
| 80 | + # Show where the spines end relative to the ticks |
| 81 | + if row > 0: |
| 82 | + label = "spines = tick positions" if nice else "spines = data min/max (misaligned)" |
| 83 | + ax.set_title(label, fontsize=7, pad=4, color="green" if nice else "red") |
| 84 | + |
| 85 | + if col == 0: |
| 86 | + ax.set_ylabel(title, fontsize=7) |
| 87 | + |
| 88 | + # Annotate the spine end-points so the numbers are visible |
| 89 | + x_lo, x_hi = ax.spines["bottom"].get_bounds() |
| 90 | + y_lo, y_hi = ax.spines["left"].get_bounds() |
| 91 | + ax.set_xlabel(f"x-spine: [{x_lo:.4g}, {x_hi:.4g}]", fontsize=7) |
| 92 | + |
| 93 | +fig.tight_layout() |
| 94 | +out_path = "results/figures/nice_bounds_demo.png" |
| 95 | +fig.savefig(out_path, dpi=150, bbox_inches="tight") |
| 96 | +print(f"Figure saved → {out_path}") |
| 97 | +plt.show() |
0 commit comments