Table of Contents
Matplotlib is a popular Python library used for creating visualizations such as graphs and charts. One of its useful features is the ability to adjust the size of markers in a plot. Markers are data points represented by symbols, such as dots or squares, and are used to highlight specific data points on a plot.
To adjust the marker size in Matplotlib, the “markersize” parameter can be used in the plot function. This parameter allows the user to specify the desired size of the markers in points (pt). The larger the value of “markersize”, the larger the markers will appear on the plot.
Overall, adjusting the marker size in Matplotlib provides flexibility in customizing visualizations and can help highlight important data points in a plot.
Adjust Marker Size in Matplotlib (With Examples)
You can use the s argument to adjust the marker size of points in Matplotlib:
plt.scatter(x, y, s=40)
The following examples show how to use this syntax in practice.
Example 1: Set a Single Marker Size for All Points
The following code shows how to create a scatterplot in Matplotlib and set a single marker size for all points in the plot:
import matplotlib.pyplotas 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 larger the number we specify for the s argument, the larger the points will be in the plot:
import matplotlib.pyplotas 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)
Example 2: Set Different Marker Sizes for Each Point
The following code shows how to create a scatterplot in Matplotlib and set a different marker size for each point in the plot:
import matplotlib.pyplotas 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)
Example 3: Use a Function to Define Marker Sizes
The following code shows how to create a scatterplot in Matplotlib and use a function to define the marker sizes to use for each point in the plot:
import matplotlib.pyplotas 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 = [3**n for n inrange(len(A))] #create scatterplot, using marker sizes specified in function plt.scatter(A, B, s=sizes)
You can find the complete Matplotlib documentation for adjusting marker size here.
Additional Resources
The following tutorials explain how to perform other common operations in Matplotlib: