Titles tell chart readers what question a plot answers before they inspect the marks. In Matplotlib, the title usually belongs to an Axes object, so the text stays attached to the plotted data when the figure is saved or shown.

The object-oriented interface keeps the title target explicit. ax.set_title() sets the title for one plot area and can position it left, center, or right; fig.suptitle() adds a heading above the whole figure when several axes share one context.

The script saves a PNG with a left-aligned axes title and a figure-level heading. layout=“constrained” gives the title area room during export, and the printed output confirms the title text without requiring an interactive window.

Steps to set a Matplotlib plot title:

  1. Save the title script as set_plot_title.py.
    set_plot_title.py
    from pathlib import Path
     
    import matplotlib.pyplot as plt
     
     
    channels = ["Web", "Retail", "Partner", "Renewal"]
    revenue = [78, 64, 52, 41]
     
    fig, ax = plt.subplots(figsize=(7, 4.2), layout="constrained")
    ax.bar(channels, revenue, color=["#4C78A8", "#59A14F", "#F28E2B", "#B07AA1"])
     
    ax.set_ylabel("Revenue ($k)")
    title = ax.set_title(
        "Monthly revenue by channel",
        loc="left",
        pad=14,
        fontweight="bold",
    )
    figure_title = fig.suptitle("Revenue dashboard", x=0.02, ha="left", fontsize=12)
     
    output = Path("monthly-revenue-title.png")
    fig.savefig(output, dpi=160)
    plt.close(fig)
     
    print(f"axes title: {ax.get_title(loc='left')}")
    print(f"axes title alignment: {title.get_ha()}")
    print(f"figure title: {figure_title.get_text()}")
    print(f"saved: {output}")
    print(f"bytes: {output.stat().st_size}")

    ax.set_title() sets one of the three axes-title positions. Use fig.suptitle() only when the whole figure needs a heading above the individual plot title.

  2. Run the script from the directory where the image should be saved.
    $ python set_plot_title.py
    axes title: Monthly revenue by channel
    axes title alignment: left
    figure title: Revenue dashboard
    saved: monthly-revenue-title.png
    bytes: 33998

    The byte count can change with Matplotlib, font, backend, or DPI differences. The title text, title alignment, figure title, and nonzero file size confirm that the script created the titled figure.

  3. Open monthly-revenue-title.png and confirm the visible title placement.

    The image should show Revenue dashboard above the figure and Monthly revenue by channel aligned with the left edge of the plot area.

  4. Change the title text, placement, and styling for your own plot.
    title = ax.set_title(
        "Monthly revenue by channel",
        loc="left",
        pad=14,
        fontweight="bold",
    )

    loc accepts left, center, or right. pad is measured in points, and text styling such as fontweight or color can be passed as keyword arguments.

  5. Remove the sample files when they were created only for testing.
    $ rm set_plot_title.py monthly-revenue-title.png