Figure dimensions decide how much room a Matplotlib plot has before it is displayed or exported. Setting the canvas width, height, and DPI deliberately keeps labels readable and makes raster image dimensions predictable for notebooks, reports, and slide decks.

Matplotlib treats figsize as the figure's physical size in inches, and raster formats such as PNG use the figure DPI to calculate pixel dimensions. A 6 inch by 3.2 inch figure saved at 150 DPI becomes a 900 by 480 pixel image unless a save option changes the canvas.

Set the size when creating a figure with figsize=(width, height) whenever possible. Use fig.set_size_inches() for a figure that already exists, and avoid bbox_inches='tight' when the exact exported canvas size matters because tight bounding can trim or expand the saved output.

Steps to set Matplotlib figure size:

  1. Choose the final width, height, and raster resolution.

    Matplotlib stores figure size in inches. Multiply width and height by DPI to predict PNG pixels, such as 6.0 x 150 = 900 pixels wide.

  2. Save the plotting script as figure_size.py.
    figure_size.py
    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}")

    layout=“constrained” asks Matplotlib to reserve room for titles, axis labels, and tick labels inside the chosen canvas.

  3. Run the script from the directory where the image should be written.
    $ python figure_size.py
    figure size: 6.00 x 3.20 in
    figure dpi: 150
    saved pixels: 900 x 480
    saved: sales-figure-size.png
  4. Open sales-figure-size.png and confirm the figure uses the requested canvas.

    The image should show a 900 x 480 pixel PNG with the title, axis labels, and grid visible inside the 6.0 x 3.2 inch figure area.

  5. Resize an existing figure before saving when the figure object was created earlier.
    fig.set_size_inches(6.0, 3.2, forward=True)
    fig.set_dpi(150)
    fig.savefig("sales-figure-size.png")

    forward=True updates the active canvas for interactive backends. In non-interactive export scripts, the saved file still uses the figure's current size and DPI.

  6. Remove the sample files when they were created only for testing.
    $ rm figure_size.py sales-figure-size.png