Matplotlib Scatter Markers

As a developer, I’ve found that data visualization is an essential skill for making sense of complex datasets. Among the many plotting tools available, Matplotlib stands out for its flexibility and power. One of the most commonly used plot types is the scatter plot, which allows you to visualize the relationship between two variables.

However, what often gets overlooked is the variety of scatter markers Matplotlib offers and how you can customize them to make your plots more insightful and visually appealing.

In this article, I’ll share my firsthand experience with Matplotlib scatter markers and guide you through different ways to use and customize them effectively.

What is a Scatter Marker in Matplotlib?

In simple terms, a scatter marker is the symbol used to represent each data point in a scatter plot. By default, Matplotlib uses a circular marker (`’o’`), but it supports many other shapes like squares, triangles, stars, and even custom markers.

Choosing the right marker can make your data easier to interpret, especially when you have overlapping points or multiple categories. Let me show you how to leverage this feature.

Basic Scatter Plot with Default Marker

To start, let’s create a simple scatter plot using data relevant to the USA, such as average annual temperatures versus electricity consumption by state.

import matplotlib.pyplot as plt

# Sample data: average temperature (°F) and electricity consumption (MWh) for five states
temperature = [58, 70, 45, 65, 55]  # Example values
electricity_consumption = [5000, 7000, 3000, 6000, 4000]

plt.scatter(temperature, electricity_consumption)
plt.title('Temperature vs Electricity Consumption')
plt.xlabel('Average Temperature (°F)')
plt.ylabel('Electricity Consumption (MWh)')
plt.show()

I executed the above example code and added the screenshot below.

matplotlib marker

This produces a basic scatter plot with circular markers.

Read Horizontal Line Matplotlib

Change Scatter Markers: Different Shapes for Different Data Points

Sometimes, you want to differentiate categories within your data. For example, you might want to distinguish coastal states from inland states. You can change the marker shape using the marker parameter.

Here are some popular marker shapes I use often:

  • 'o' : Circle (default)
  • 's' : Square
  • '^' : Triangle up
  • 'v' : Triangle down
  • '*' : Star
  • 'D' : Diamond
  • 'P' : Plus (filled)
  • 'X' : X marker

Example:

coastal_states = [True, True, False, False, True]

for i in range(len(temperature)):
    if coastal_states[i]:
        plt.scatter(temperature[i], electricity_consumption[i], marker='^', color='blue')
    else:
        plt.scatter(temperature[i], electricity_consumption[i], marker='s', color='green')

plt.title('Temperature vs Electricity Consumption by State Type')
plt.xlabel('Average Temperature (°F)')
plt.ylabel('Electricity Consumption (MWh)')
plt.show()

I executed the above example code and added the screenshot below.

matplotlib markers

This way, you can visually separate data points based on categories.

Check out Draw a Vertical Line Matplotlib

Customize Marker Size and Color

Marker size and color can add another layer of information. For instance, you could use marker size to represent population size and color to indicate urbanization rate.

population = [10, 39, 5, 20, 15]  # in millions (example)
urbanization_rate = [85, 90, 70, 80, 75]  # percentage

plt.scatter(temperature, electricity_consumption,
            s=[p * 10 for p in population],  # scale size by population
            c=urbanization_rate,              # color by urbanization rate
            cmap='viridis', alpha=0.7)

plt.colorbar(label='Urbanization Rate (%)')
plt.title('Temperature vs Electricity Consumption with Population and Urbanization')
plt.xlabel('Average Temperature (°F)')
plt.ylabel('Electricity Consumption (MWh)')
plt.show()

I executed the above example code and added the screenshot below.

matplotlib scatter marker

Here, marker size and color add depth to your visualization, making it easier to extract insights.

Read Matplotlib Two y axes

Use Custom Markers

Matplotlib also supports custom markers using a tuple or a path. This is useful if you want your scatter plot to stand out or match a specific theme.

For example, let’s create a star-shaped marker with a custom size:

import numpy as np
from matplotlib.markers import MarkerStyle

star_marker = MarkerStyle('*')

plt.scatter(temperature, electricity_consumption, marker=star_marker, s=200, color='red')
plt.title('Custom Star Markers')
plt.xlabel('Average Temperature (°F)')
plt.ylabel('Electricity Consumption (MWh)')
plt.show()

You can also define completely custom markers by passing a tuple with vertices, but that’s more advanced and less commonly needed.

Read Matplotlib Invert y axis

Combine Multiple Marker Styles in One Plot

In real-world projects, I often need to plot multiple datasets on the same scatter plot with different markers, sizes, and colors to compare them visually.

Here’s an example comparing electricity consumption for coastal vs inland states:

coastal_temp = [58, 70, 55]
coastal_elec = [5000, 7000, 4000]

inland_temp = [45, 65]
inland_elec = [3000, 6000]

plt.scatter(coastal_temp, coastal_elec, marker='o', color='blue', label='Coastal States')
plt.scatter(inland_temp, inland_elec, marker='s', color='orange', label='Inland States')

plt.title('Electricity Consumption by State Type')
plt.xlabel('Average Temperature (°F)')
plt.ylabel('Electricity Consumption (MWh)')
plt.legend()
plt.show()

Using distinct markers and colors helps viewers quickly understand the data groups.

Check out Put the Legend Outside the Plot in Matplotlib

Tips for Using Scatter Markers Effectively

  • Avoid clutter: If your dataset is large, too many markers can overlap. Use transparency (alpha) or smaller sizes.
  • Consistent legend: Always include a legend when using different markers to clarify what each represents.
  • Colorblind-friendly palettes: Use palettes that are accessible to everyone.
  • Size scaling: When using marker sizes to represent data, choose a scale that makes differences visible but not overwhelming.

Mastering Matplotlib scatter markers is a simple yet powerful way to make your data visualizations more informative and aesthetically pleasing. Whether you’re analyzing state-level data in the USA or any other dataset, customizing your scatter plot markers will help you communicate your insights.

If you’re interested in diving deeper, I recommend experimenting with marker edge colors, colormaps, and interactive plots to further enhance your visual storytelling.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.