Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to change marker size in scatter plot matplotlib

Understanding Scatter Plots

Before we dive into changing marker sizes, let's quickly understand what scatter plots are. Imagine you have a bunch of stickers and a blank chart. If you place these stickers on the chart based on two features, such as the sweetness and size of different fruits, you get a visual representation of how these features relate across different fruits. That's essentially what a scatter plot is—a graph that depicts the relationship between two variables by displaying data points.

Getting Started with Matplotlib

To create a scatter plot in Python, we use a library called Matplotlib, which is a popular plotting library that provides a MATLAB-like interface for making graphs and charts. It's like a digital art kit for data scientists and programmers. To get started, you need to install Matplotlib if you haven't already. This can be done using pip, Python's package installer:

pip install matplotlib

After the installation, you can import the pyplot module from Matplotlib, which is commonly imported as plt for ease of use:

import matplotlib.pyplot as plt

Creating a Basic Scatter Plot

Let's create a basic scatter plot. We need two lists: one for the x-axis (horizontal) values and one for the y-axis (vertical) values.

# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

# Creating a scatter plot
plt.scatter(x, y)
plt.show()

When you run this code, a window will pop up displaying a scatter plot with default marker sizes.

Customizing Marker Size

Now, let's learn how to customize the size of the markers. In Matplotlib, you can change the marker size using the s parameter in the scatter function.

# Changing marker sizes
marker_sizes = [20, 50, 100, 200, 500]

plt.scatter(x, y, s=marker_sizes)
plt.show()

In this example, each point will have a different size corresponding to the values in the marker_sizes list. The size is measured in points^2, where one point is 1/72 of an inch.

Intuition Behind Marker Sizes

Think of the marker size as the volume of your voice. When you're trying to emphasize a point in a conversation, you might speak louder. Similarly, in a scatter plot, you might want to make a marker bigger to emphasize that data point.

Setting Uniform Marker Sizes

If you want all markers to have the same size, you can pass a single number to the s parameter.

# All markers with the same size
uniform_size = 50

plt.scatter(x, y, s=uniform_size)
plt.show()

Now, all the markers will have the same size, making them look uniform.

Dynamic Marker Sizes Based on Data

Sometimes, you might want the size of the markers to reflect another variable in your data. For instance, if you're plotting fruit sizes against sweetness, you might want the marker size to represent the fruit's weight.

# Dynamic marker sizes based on another variable
weights = [30, 45, 10, 55, 25]

plt.scatter(x, y, s=weights)
plt.show()

Here, the weights list represents the third variable, and the markers' sizes on the plot will scale accordingly.

Styling the Markers

Beyond size, you can also change the style of the markers. This includes the edge color, face color, and marker shape.

# Custom marker styles
plt.scatter(x, y, s=weights, color='green', edgecolor='black', marker='^')
plt.show()

In this snippet, color changes the fill color of the markers, edgecolor changes the border color, and marker changes the shape (e.g., '^' for triangles).

Understanding Marker Size Limitations

It's important to note that if you make the markers too large, they might overlap and make the plot hard to read. On the flip side, if they are too small, they might be difficult to see. Finding the right size is about balance and depends on the context of your data and what you're trying to convey.

Saving Your Scatter Plot

Once you're satisfied with your scatter plot, you can save it to your computer using the savefig function.

plt.scatter(x, y, s=weights)
plt.savefig('my_scatter_plot.png')

This will save your scatter plot as a PNG image in the current directory.

Conclusion

Scatter plots are a powerful tool for visualizing the relationship between two variables, and customizing marker sizes can add an additional layer of information. By adjusting the size of each point, you can convey the weight, importance, or frequency of data points without cluttering the graph with additional axes or overwhelming the viewer with numbers.

Remember, the key to an effective scatter plot is not just in the data it presents but in how easily that data can be understood. By thoughtfully choosing your marker sizes, you can create a plot that tells a story at a glance, like how a well-chosen font can make a quote stand out in a sea of text. So, use the power of Matplotlib to not just show data, but to tell a visual story that resonates with your audience.

Experiment with different sizes and styles to find what best represents your data. With Matplotlib and a bit of creativity, your scatter plots can become not just informative, but also visually appealing. Happy plotting!