I’ve worked extensively with data visualization libraries, and Matplotlib remains one of my go-to tools. Scatter plots are particularly useful when you want to visualize the relationship between two variables, especially in fields like marketing analytics or healthcare data analysis, here in the USA.
One key element that often gets overlooked but is crucial for clarity is the legend. A well-crafted legend helps your audience understand what different colors, shapes, or sizes in your scatter plot represent.
In this article, I’ll walk you through how to add and customize legends in Matplotlib scatter plots, sharing practical tips from my own experience.
Add a Basic Legend to a Scatter Plot
Let’s start with the simplest way to add a legend. Suppose you have three different customer segments, and you want to plot each with a distinct color.
import matplotlib.pyplot as plt
# Sample data
age_groups = ['18-25', '26-35', '36-45']
income = [45000, 54000, 58000]
spending_score = [60, 70, 65]
# Plotting each group
plt.scatter(income[0], spending_score[0], color='red', label='18-25')
plt.scatter(income[1], spending_score[1], color='blue', label='26-35')
plt.scatter(income[2], spending_score[2], color='green', label='36-45')
# Adding legend
plt.legend()
plt.xlabel('Annual Income ($)')
plt.ylabel('Spending Score')
plt.title('Customer Segments by Age Group')
plt.show()I executed the above example code and added the screenshot below.

Here, the label parameter in each scatter() call assigns a label to the points. Calling plt.legend() then adds the legend to the plot automatically.
Customize Legend Location
By default, Matplotlib places the legend in the “best” location. But sometimes you want to control where it appears, especially if the default overlaps with your data points.
You can specify the location using the loc parameter:
plt.legend(loc='upper right')Some common location options include:
'best'(default)'upper right''upper left''lower left''lower right''center'
In my experience, placing the legend outside the plot area can improve clarity when you have many data points:
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))I executed the above example code and added the screenshot below.

This places the legend to the right of the plot, centered vertically.
Use Legend Titles for Better Context
When you have multiple categories, adding a title to your legend can help clarify what the legend items represent.
plt.legend(title='Age Groups')This small addition enhances professionalism and makes your plot easier to interpret.
Handle Multiple Scatter Plots with Different Markers
Sometimes, you may want to differentiate groups not just by color but also by marker style — for example, circles, squares, and triangles.
plt.scatter(income[0], spending_score[0], color='red', marker='o', label='18-25')
plt.scatter(income[1], spending_score[1], color='blue', marker='s', label='26-35')
plt.scatter(income[2], spending_score[2], color='green', marker='^', label='36-45')
plt.legend()
plt.show()I executed the above example code and added the screenshot below.

The legend will automatically reflect both color and marker differences.
Control Legend Font Size and Style
To make your legend fit better with your plot’s design, you can customize font size and style:
plt.legend(fontsize='large', title='Age Groups', title_fontsize='medium')This is especially useful when preparing plots for presentations or reports where readability is key.
Advanced: Create Custom Legend Handles
In some scenarios, you might want to create a legend independent of the plotted data, such as when plotting multiple datasets or custom shapes.
Here’s how to create custom legend entries:
import matplotlib.patches as mpatches
red_patch = mpatches.Patch(color='red', label='18-25')
blue_patch = mpatches.Patch(color='blue', label='26-35')
green_patch = mpatches.Patch(color='green', label='36-45')
plt.legend(handles=[red_patch, blue_patch, green_patch], title='Age Groups')This method gives you full control over legend entries, useful for complex visualizations.
Tips from Real-World Projects
In a recent project analyzing retail sales across multiple US states, I used scatter plots to visualize sales versus customer footfall, differentiating stores by region with color and marker styles. Customizing the legend location and adding a title helped stakeholders quickly grasp the segmentation without confusion.
Remember, always test your plots on different screen sizes and mediums to ensure legends remain clear and don’t obstruct important data.
Adding and customizing legends in Matplotlib scatter plots is easy but essential for effective data storytelling. Whether you’re sharing insights with your team or presenting to executives, clear legends make your plots more professional and accessible.
You may like to read:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.