How to Adjust Marker Size in Matplotlib?
Last Updated :
31 May, 2025
In Matplotlib, adjusting marker size improves the clarity and visual impact of data points. Marker size controls how large points appear and can be set uniformly or varied per point to highlight differences or represent extra data. This flexibility enhances both simple and detailed visualizations effectively.
Using plot()
plot() method is mainly used for line graphs but allows adding markers at data points. You can customize the marker size using the markersize parameter. However, it only supports uniform marker sizes, unless you loop through each point manually.
Example 1: In this example, we make a line plot with uniform marker size using markersize. All points are shown with the same-sized circular markers.
Python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 2]
plt.plot(x, y, marker='o', markersize=12, linestyle='-') # same size
plt.title("Line Plot with Uniform Marker Size")
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
Output
Using plot()Explanation: plot() creates a line plot with circular markers marker='o' of uniform size markersize=12 connected by a solid line linestyle='-'.
Example 2: In this example, we make a line plot with custom marker sizes for each point. We loop through the data and assign a different markersize
to each point manually.
Python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 2]
sizes = [5, 10, 15, 20, 25] # Custom size
for i in range(len(x)):
plt.plot(x[i], y[i], marker='o', markersize=sizes[i], color='blue')
plt.title("Custom Marker Sizes with plot()")
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
Output
Using plot()Explanation: plot() in a loop to create individual points with circular markers marker='o' of custom sizes markersize=sizes[i] . Each point is plotted separately, allowing different marker sizes for each data point.
Using scatter()
scatter() method is specifically designed for plotting individual data points. It lets you easily set different marker sizes using the s parameter as a list. This is useful when marker size represents data values like weight, population, or intensity.
Example 1: In this example, we create a scatter plot with varying marker sizes. Each point has a different size based on the sizes list using the s parameter.
Python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 2]
sizes = [20, 50, 80, 200, 500] # Size per point
plt.scatter(x, y, s=sizes, c='green')
plt.title("Scatter Plot with Varying Marker Sizes")
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
Output
Using scatter()Explanation: scatter() create a plot where each point is represented with a circular marker of a different size s=sizes. c='green' sets all marker colors to green and marker sizes vary based on the sizes list.
Example 2: In this example, marker sizes are derived from actual data values. We scale the data to control the marker size and visualize the magnitude of each point.
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 3, 1, 4, 2])
data_value = np.array([100, 300, 200, 600, 900]) # data features
sizes = data_value / 2 # Scale them down
plt.scatter(x, y, s=sizes, c='red')
plt.title("Marker Size Derived from Data Values")
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
Output
Using scatter()Explanation: scatter() creates a plot with marker sizes based on data values. The data_value array is scaled down to set sizes and points are displayed in red c='red', visually representing the data magnitude.