matplotlib: match legend text color with symbol in scatter plot

Phyinmi

I made a scatter plot with 3 different colors and I want to match the color of the symbol and the text in the legend.

A nice solution exist for the case of line plots:

leg = ax.legend()

# change the font colors to match the line colors:
for line,text in zip(leg.get_lines(), leg.get_texts()):
    text.set_color(line.get_color())

However, scatter plot colors cannot be accessed by get_lines().For the case of 3 colors I think I can manually set the text colors one-by-one using eg. text.set_color('r'). But I was curious if it can be done automatically as lines. Thanks!

Y. Luo

This seems complicated but does give you what you want. Suggestions are welcomed. I use ax.get_legend_handles_labels() to get the markers and use tuple(handle.get_facecolor()[0]) to get the matplotlib color tuple. Made an example with a really simple scatter plot like this:

Edit:

As ImportanceOfBeingErnest pointed in his answer:

  1. leg.legendHandles will return the legend handles;
  2. List, instead of tuple, can be used to assign matplotlib color.

Codes are simplified as:

import matplotlib.pyplot as plt
from numpy.random import rand


fig, ax = plt.subplots()
for color in ['red', 'green', 'blue']:
    x, y = rand(2, 10)
    ax.scatter(x, y, c=color, label=color)

leg = ax.legend()
for handle, text in zip(leg.legendHandles, leg.get_texts()):
    text.set_color(handle.get_facecolor()[0])

plt.show()

What I got is: enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Matplotlib scatter plot color-coded by text, how to add legend?

Matplotlib scatter plot with legend

Matplotlib scatter plot legend

matplotlib scatter plot with color label and legend specified by c option

Matplotlib - Adding legend to scatter plot

Matplotlib scatter plot different colors in legend and plot

Why doesn't the color of the points in a scatter plot match the color of the points in the corresponding legend?

color issue in scatter plot with matplotlib

matplotlib: scatter plot with legend as string of class

Matplotlib: How to add legend to scatter plot colors?

Matplotlib scatter plot legend display problem

How to add a legend to matplotlib scatter plot

scatter plot matplotlib results in same color

removing outline color of scatter plot in matplotlib python

matplotlib subplot alignment with scatter plot and color bar

Different color for each set in scatter plot on matplotlib

Specify color of each point in scatter plot (matplotlib)

Matplotlib - usage of Scatter plot with specific color assigment

Matplotlib: How to combine scatter and line plot to one legend entry

Add legend to a matplotlib scatter plot where colors are dynamic

Pandas Matplotlib: How to change shape and size of the legend in scatter plot?

Matplotlib scatter plot with different text at each point

plot scatter with x, y title, legend based on different color

Scatter plot legend shows only one variable with color -Pandas Seaborn

pandas - scatter plot with different color legend for each point

MATLAB scatter plot legend

How to plot different shades of a color for each point in a scatter plot in matplotlib?

nvd3.js - unable to change color of symbol in scatter plot

Using matplotlib scatter markers in text of legend together with Latex