Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use ax.scatter(x, y) to plot paired observations, add your labels and title, then call fig.tight_layout() for a simple one-time layout adjustment. If your figure includes a legend, colorbar, or a more complex grid, start with Matplotlib’s constrained layout instead. Neither option guarantees a perfect result in every figure, so inspect the displayed or saved output.

Make a scatter plot and adjust its layout

This example plots five paired values, gives every point the same color, and asks Matplotlib to adjust the subplot spacing after the labels and title are in place:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 1, 4, 3, 5]

fig, ax = plt.subplots()
ax.scatter(x, y, s=40, color="tab:blue", alpha=0.8)
ax.set_xlabel("X value")
ax.set_ylabel("Y value")
ax.set_title("Example scatter plot")
fig.tight_layout()
plt.show()

The x and y sequences provide the horizontal and vertical positions of the points. s=40 sets marker area in points squared, not the marker radius. color applies one uniform color, while alpha controls transparency. The scatter API and its options are documented in the Matplotlib scatter reference.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the layout method that fits your figure

Method When to enable it What it accommodates Practical limitation
tight_layout() Call it after adding plot elements when you want a one-time adjustment. Primarily tick labels, axis labels, and titles. Its checks are limited, and crowded or unusual figures may still clip or overlap.
Constrained layout Enable it when creating the figure, before adding Axes. Labels and titles, plus decorations such as legends and colorbars; it is more flexible for complex grids. Inspect crowded or unusual figures to confirm the result is suitable.

Matplotlib’s documentation describes constrained layout as the more modern and capable option that should typically be used instead of tight layout. See the tight layout guide and the constrained layout guide for details.

Use tight_layout for a straightforward figure

fig.tight_layout() adjusts subplot parameters when you call it; it does not ordinarily recalculate spacing on every redraw. Add the decorations first, then call it. You can also pass pad, w_pad, and h_pad to control extra spacing; the documented padding values are fractions of the font size. Avoid pad=0 if text is close to the edge, because the guide warns that it can clip text by a few pixels and recommends padding above 0.3.

Tight layout checks a limited set of decorations, and its behavior may vary slightly across repeated calls because the algorithm does not necessarily converge. The guide also notes that artists can be excluded from layout calculations with Artist.set_in_layout. Treat the adjustment as a useful first pass, not a guarantee: inspect the figure, including the saved version if that is what you plan to publish or share.

Use constrained layout for more complex figures

Enable constrained layout when constructing the figure and do not call tight_layout() afterward:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 1, 4, 3, 5]

fig, ax = plt.subplots(layout="constrained")
ax.scatter(x, y)
ax.set_xlabel("X value")
ax.set_ylabel("Y value")
ax.set_title("Example scatter plot")
plt.show()

Calling tight_layout() turns constrained layout off. Constrained layout is generally the better starting point for figures with legends, colorbars, or multiple Axes, but the final spacing still deserves a visual check.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Encode a third variable with color or size

A scatter plot can use marker size or color to represent an additional variable. For a numeric variable mapped to color, pass its values as c; use cmap to choose a colormap and norm to control how values map onto it. The API also supports marker shape, transparency, edge colors, and line widths.

For one uniform color, prefer color="tab:blue" over a single numeric RGB(A) sequence passed as c: Matplotlib can interpret numeric c values as data to map through a colormap. Marker edge lines are centered on the shape boundary, so a positive edge linewidth can make small markers appear larger. To remove that outline, use linewidths=0 or edgecolors="none".

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.