How to create a scatter plot in Matplotlib

Scatter plots show how paired numeric values move together without connecting points into a sequence. In Matplotlib, they are useful for spotting clusters, outliers, and relationships between two measurements while marker color or size carries extra variables.

The object-oriented interface keeps the plot elements explicit. plt.subplots() returns the Figure and Axes objects, ax.scatter() draws the markers, and fig.colorbar() adds a scale when numeric values are mapped to color.

A small ad-spend dataset makes the marker positions, color scale, and saved PNG visible before substituting real data. The printed output records the title, axis labels, colorbar label, and image filename produced by the same script.

Steps to create a Matplotlib scatter plot:

  1. Save the scatter plot script as create_scatter_plot.py.
    create_scatter_plot.py
    from pathlib import Path
     
    import matplotlib.pyplot as plt
     
    ad_spend = [120, 180, 240, 310, 380, 440, 520, 600]
    sales = [980, 1250, 1420, 1680, 1890, 2100, 2380, 2650]
    conversion_rate = [2.1, 2.4, 2.8, 3.0, 3.2, 3.4, 3.7, 3.9]
    orders = [24, 31, 35, 42, 48, 54, 61, 68]
    marker_size = [order * 6 for order in orders]
     
    fig, ax = plt.subplots(layout="constrained")
    scatter = ax.scatter(
        ad_spend,
        sales,
        c=conversion_rate,
        s=marker_size,
        cmap="viridis",
        alpha=0.85,
        edgecolors="black",
        linewidths=0.5,
    )
    colorbar = fig.colorbar(scatter, ax=ax, label="Conversion rate (%)")
     
    ax.set_title("Ad spend compared with daily sales")
    ax.set_xlabel("Ad spend (USD)")
    ax.set_ylabel("Sales (USD)")
    ax.grid(True, alpha=0.25)
     
    output = Path("campaign-scatter-plot.png")
    fig.savefig(output, dpi=160)
    plt.close(fig)
     
    print(f"points: {len(ad_spend)}")
    print(f"title: {ax.get_title()}")
    print(f"x label: {ax.get_xlabel()}")
    print(f"y label: {ax.get_ylabel()}")
    print(f"colorbar label: {colorbar.ax.get_ylabel()}")
    print(f"saved: {output}")
    print(f"bytes: {output.stat().st_size}")

    c maps numeric values to colors through the colormap, and s sets marker area in points squared. Scale large real-world counts before passing them to s so the markers remain readable.

  2. Run the script from the Python environment that has Matplotlib installed.
    $ python create_scatter_plot.py
    points: 8
    title: Ad spend compared with daily sales
    x label: Ad spend (USD)
    y label: Sales (USD)
    colorbar label: Conversion rate (%)
    saved: campaign-scatter-plot.png
    bytes: 62517

    The byte count can change with Matplotlib, font, backend, or DPI differences. A nonzero byte count and the expected filename confirm that the PNG was written.

  3. Open campaign-scatter-plot.png and confirm the plotted result.

    The image should show eight markers, axis labels for ad spend and sales, and a colorbar labeled Conversion rate (%).

  4. Replace the sample lists with paired numeric values from your own data.
    ad_spend = [120, 180, 240, 310]
    sales = [980, 1250, 1420, 1680]
    conversion_rate = [2.1, 2.4, 2.8, 3.0]
    orders = [24, 31, 35, 42]

    The x and y sequences must have the same length. When c or s contains one value per point, those sequences must match the same point count.

  5. Remove the sample files when they were created only for testing.
    $ rm create_scatter_plot.py campaign-scatter-plot.png