from pathlib import Path from PIL import Image import matplotlib.pyplot as plt width_in = 6.0 height_in = 3.2 dpi = 150 quarters = ["Q1", "Q2", "Q3", "Q4"] revenue = [42, 51, 58, 64] fig, ax = plt.subplots(figsize=(width_in, height_in), dpi=dpi, layout="constrained") ax.plot(quarters, revenue, marker="o", linewidth=2.0) ax.set_title("Quarterly revenue") ax.set_xlabel("Quarter") ax.set_ylabel("Revenue (USD thousands)") ax.grid(True, axis="y", alpha=0.25) output = Path("sales-figure-size.png") fig.savefig(output) plt.close(fig) with Image.open(output) as image: pixels = image.size size = fig.get_size_inches() print(f"figure size: {size[0]:.2f} x {size[1]:.2f} in") print(f"figure dpi: {fig.dpi:.0f}") print(f"saved pixels: {pixels[0]} x {pixels[1]}") print(f"saved: {output}")