Learning Matplotlib: How to Change Marker Size in Scatter Plots


When conducting data visualization using the powerful Matplotlib library in Python, controlling the visual characteristics of your data points is essential for clarity and impact. One of the most frequently adjusted parameters in a scatterplot is the size of the markers. You can use the dedicated argument, designated as s, within the plt.scatter() function to precisely control the marker size of all points plotted.

The s parameter accepts either a single scalar value, which applies uniformly to every point, or an array-like structure where each element corresponds to the size of an individual data point. This flexibility allows for the creation of simple scatter visualizations or more complex bubble charts.

The fundamental syntax for applying a uniform size adjustment looks like this:

plt.scatter(x, y, s=40) 

The following comprehensive examples demonstrate how to implement this syntax effectively in practice, illustrating both uniform and variable marker sizing techniques.

Understanding Marker Sizing in Matplotlib’s `scatter` Function

The plt.scatter function is the primary tool in Matplotlib for plotting individual data points defined by their X and Y coordinates. Unlike the plt.plot function, which is designed for continuous lines, plt.scatter offers granular control over the properties of each marker, including color, shape, and crucially, size. The s argument is used to specify the size of the marker in squared points. This means that if you double the value of s, you quadruple the area of the marker. Understanding this relationship is important for achieving proportional scaling in visualizations where size represents a third dimension of data.

Proper marker sizing contributes significantly to the interpretability of a scatterplot. If markers are too small, they may be difficult to distinguish or lead to misinterpretation of density. Conversely, if markers are excessively large, they can overlap severely, obscuring underlying patterns or clustering. Therefore, choosing an appropriate scale for the s value is a critical step in the data visualization pipeline. The value itself is unitless within the Matplotlib framework, so experimentation is often required to find the optimal visual balance for a given plot size and data distribution.

Example 1: Setting a Consistent Marker Size for All Data Points

The simplest application of the s argument is setting a single, uniform size across all data points in the plot. This is typically done when marker size does not convey any specific data variable but simply needs to be adjusted for visual clarity or aesthetic preference. The following Python code demonstrates how to define two simple arrays, A and B, and generate a scatterplot where all markers possess a size value of 40.

We start by importing the necessary module, matplotlib.pyplot, conventionally aliased as plt. We then define our coordinate data sets (A and B). Finally, we invoke the plt.scatter function, providing A and B as the X and Y coordinates, respectively, and setting the marker size using s=40. This scalar input ensures every point is rendered identically in terms of size.

import matplotlib.pyplot as plt

#define two arrays for plotting
A = [3, 5, 5, 6, 7, 8]
B = [12, 14, 17, 20, 22, 27]

#create scatterplot, specifying marker size to be 40
plt.scatter(A, B, s=40)

The resulting visualization shows six distinct data points, all rendered with the same moderate size, optimizing visibility without excessive overlap:

Marker size in Matplotlib

Visualizing Scale Differences: Impact of the `s` Argument

To illustrate the direct impact of the s argument value, we can rerun the previous example while significantly increasing the scalar value assigned to s. As mentioned, the value represents the area of the marker in squared points; thus, increasing this number results in a geometrically larger visualization of each data point. This demonstration is vital for understanding how to adjust marker visibility for larger or smaller datasets, or when preparing plots for presentation versus print media.

In the code below, we utilize the exact same coordinate arrays (A and B), ensuring a direct comparison to the previous output. However, we set the s argument to 120—three times larger than the previous setting. This adjustment dramatically increases the visual prominence of the data points, although it also introduces slight overlap, particularly between the second and third points where the x-coordinate is identical (x=5). This emphasizes the trade-off between visibility and potential occlusion.

import matplotlib.pyplot as plt

#define two arrays for plotting
A = [3, 5, 5, 6, 7, 8]
B = [12, 14, 17, 20, 22, 27]

#create scatterplot, specifying marker size to be 120
plt.scatter(A, B, s=120)

As expected, the resulting graph displays significantly larger markers. The larger the numerical value specified for the s argument, the larger the points will be rendered in the plot. Observing this output helps calibrate the appropriate size for any given visualization, ensuring that the visual elements support, rather than detract from, the data analysis.

Markersize adjustment in scatterplot of Matplotlib

Example 2: Assigning Unique Marker Sizes to Individual Data Points

Beyond setting a static size, one of the most powerful features of the plt.scatter function is its ability to accept an array of sizes for the s argument. When an array is passed, Matplotlib interprets each value in the array as the size corresponding to the point at the same index in the X and Y coordinate arrays. This technique is fundamental for creating bubble charts, where marker size itself encodes a third variable (or dimension) of the dataset, providing richer contextual information.

For this example, we define a third array, sizes, which contains six distinct values corresponding to the six data points in arrays A and B. It is critical that the length of the size array exactly matches the length of the coordinate arrays; otherwise, Matplotlib will raise an error. The values in the sizes array range from 20 to 150, ensuring a visible gradient in point size across the visualization.

import matplotlib.pyplot as plt

#define two arrays for plotting
A = [3, 5, 5, 6, 7, 8]
B = [12, 14, 17, 20, 22, 27]

#define array of marker sizes to use
sizes = [20, 40, 60, 80, 100, 150]

#create scatterplot, using marker sizes specified in array
plt.scatter(A, B, s=sizes)

The resulting scatterplot visually maps the size variable onto the markers. As the index increases (moving generally from left to right along the x-axis), the markers progressively increase in size, allowing the viewer to instantly grasp the relationship between the X/Y coordinates and the encoded size dimension. This is a highly effective method for multi-dimensional data visualization.

Different marker sizes in Matplotlib scatterplot

Example 3: Defining Marker Sizes Dynamically Using a Function

While manually defining a list of sizes (as shown in Example 2) is feasible for small datasets, real-world data analysis often requires calculating marker sizes programmatically based on a formula, an underlying distribution, or even the index of the data point itself. Utilizing a function or a list comprehension in Python provides a highly efficient and scalable solution for generating the size array dynamically.

The following code demonstrates how to use a simple exponential function to define marker sizes. We iterate through the indices of array A and calculate the size of each marker as 3 raised to the power of its index (n). This creates a non-linear progression of sizes, where later points are exponentially larger than earlier points. This highlights the power of coupling array indexing with mathematical operations to generate sophisticated visual scaling.

import matplotlib.pyplot as plt

#define two arrays for plotting
A = [3, 5, 5, 6, 7, 8]
B = [12, 14, 17, 20, 22, 27]

#define array of marker sizes to use (3^n where n is the index)
sizes = [3**n for n in range(len(A))]

#create scatterplot, using marker sizes specified in function
plt.scatter(A, B, s=sizes)

When plotting the results using the plt.scatter function, the exponential growth in size becomes immediately apparent. The final data point, calculated as 3^5 (or 243), is significantly larger than the initial points, vividly demonstrating the effect of non-linear scaling on the visualization. This method is particularly useful when the data variable intended for size scaling exhibits a wide range or skewed distribution.

Marker size function in Matplotlib in Python

Best Practices and Considerations for Marker Sizing

While the technical implementation of marker sizing in Matplotlib is straightforward, strategic decisions about size scaling are crucial for effective data visualization. When using marker size to represent a third variable (a bubble chart), it is highly recommended to include a legend or a color bar that explicitly maps the size dimension to the corresponding data values. Without this context, the viewer cannot accurately interpret the meaning of the size variation.

Furthermore, managing overlap, especially in dense datasets, is a key concern. If large markers obscure crucial data points, consider adjusting the transparency of the markers using the alpha argument within plt.scatter. Setting alpha to a value between 0 (fully transparent) and 1 (fully opaque) can reveal underlying data structure even when points overlap significantly. For extremely large datasets, techniques such as binning or utilizing specialized visualization libraries optimized for density plotting may be necessary alternatives to simple scatter markers.

Finally, remember the relationship between the s parameter and the marker area. If you intend for the marker radius or diameter to be linearly proportional to the data variable, you must square the data values before passing them to the s argument. If you pass the raw data values directly to s, the area of the marker will be linearly proportional to the data, which often leads to the visual perception of under-representation for smaller values and over-representation for larger values.

Additional Resources

For more detailed information regarding the plt.scatter function and all its available parameters, including color mapping, edge colors, and marker styles, please refer to the official Matplotlib documentation.

The following tutorials explain how to perform other common operations in Matplotlib, allowing you to further customize your plots:

Cite this article

Mohammed looti (2025). Learning Matplotlib: How to Change Marker Size in Scatter Plots. PSYCHOLOGICAL STATISTICS. Retrieved from https://statistics.arabpsychology.com/adjust-marker-size-in-matplotlib-with-examples/

Mohammed looti. "Learning Matplotlib: How to Change Marker Size in Scatter Plots." PSYCHOLOGICAL STATISTICS, 6 Nov. 2025, https://statistics.arabpsychology.com/adjust-marker-size-in-matplotlib-with-examples/.

Mohammed looti. "Learning Matplotlib: How to Change Marker Size in Scatter Plots." PSYCHOLOGICAL STATISTICS, 2025. https://statistics.arabpsychology.com/adjust-marker-size-in-matplotlib-with-examples/.

Mohammed looti (2025) 'Learning Matplotlib: How to Change Marker Size in Scatter Plots', PSYCHOLOGICAL STATISTICS. Available at: https://statistics.arabpsychology.com/adjust-marker-size-in-matplotlib-with-examples/.

[1] Mohammed looti, "Learning Matplotlib: How to Change Marker Size in Scatter Plots," PSYCHOLOGICAL STATISTICS, vol. X, no. Y, ص Z-Z, November, 2025.

Mohammed looti. Learning Matplotlib: How to Change Marker Size in Scatter Plots. PSYCHOLOGICAL STATISTICS. 2025;vol(issue):pages.

Download Post (.PDF)
Scroll to Top