How to Adjust Marker Size in Matplotlib?

In a plot, a marker is a symbol that designates a single data point. Size, color, and shape are just a few of the attributes that may be changed. Markers are commonly used in conjunction with other charting methods to enhance the readability and comprehension of data.

With Matplotlib, a wide variety of marker shapes are provided, including circles, squares, triangles, diamonds, and more. It is possible to alter the marker size to draw attention to crucial details or to develop more aesthetically pleasing plots. We'll show you how to alter the marker size in Matplotlib using examples of Python code in this post.

Syntax

The syntax to adjust marker size in Matplotlib is as follows

plt.scatter(x_values, y_values, s=marker_size)

Here, the s parameter specifies the marker size.

Basic Example

Here's a simple example showing how to create a scatter plot with custom marker size

import matplotlib.pyplot as plt
import numpy as np

# Set x and y values
x = np.array([1, 2, 3, 4, 5])
y = np.array([6, 7, 8, 9, 10])

# Set marker size
marker_size = 100

# Plot the data with custom marker size
plt.scatter(x, y, s=marker_size)
plt.title("Scatter Plot with Custom Marker Size")
plt.xlabel("X values")
plt.ylabel("Y values")
plt.show()

The output shows a scatter plot with markers of size 100 pixels.

Variable Marker Sizes

You can also vary the marker size for each point by passing an array to the s parameter

import matplotlib.pyplot as plt
import numpy as np

# Define x, y, and size values
x = np.array([1, 2, 3, 4, 5])
y = np.array([6, 7, 8, 9, 10])
sizes = np.array([20, 40, 60, 80, 100])

# Plot the data with varying marker sizes
plt.scatter(x, y, s=sizes)
plt.title("Scatter Plot with Variable Marker Sizes")
plt.xlabel("X values")
plt.ylabel("Y values")
plt.show()

In this example, each marker has a different size corresponding to the values in the sizes array.

Combining Size and Color

You can combine variable marker sizes with colors for more informative plots

import matplotlib.pyplot as plt
import numpy as np

# Sample data
x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([2, 5, 3, 8, 7, 6])
sizes = np.array([30, 60, 90, 120, 150, 180])
colors = np.array([10, 20, 30, 40, 50, 60])

# Create scatter plot with variable sizes and colors
plt.scatter(x, y, s=sizes, c=colors, cmap='viridis', alpha=0.7)
plt.colorbar(label='Color Scale')
plt.title("Scatter Plot with Variable Sizes and Colors")
plt.xlabel("X values")
plt.ylabel("Y values")
plt.show()

This creates a more complex visualization where both size and color represent different data dimensions.

Marker Size in Line Plots

For line plots with markers, use the markersize parameter

import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 6, 8, 10])

# Plot with custom marker size
plt.plot(x, y, marker='o', markersize=10, linestyle='-', linewidth=2)
plt.title("Line Plot with Custom Marker Size")
plt.xlabel("X values")
plt.ylabel("Y values")
plt.grid(True, alpha=0.3)
plt.show()

The markersize parameter controls the size of markers in line plots.

Common Use Cases

Use Case Application Example
Financial Data Market capitalization Larger markers for higher market cap
Scientific Research Sample sizes Marker size represents study participants
Geographic Data Population density City markers sized by population

Conclusion

Adjusting marker size in Matplotlib is essential for creating informative data visualizations. Use the s parameter in scatter() for scatter plots and markersize for line plots. Variable marker sizes can represent additional data dimensions, making your plots more meaningful and visually appealing.

Updated on: 2026-03-27T00:41:48+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements