How to add legend based on color in scatterplot [matplotlib]

Necronet

I have been trying to add legend based on the color in a scatter plot in matplotlib. I have seen several example but they assume that each plot will have a label associated to it, and I haven't figure out how to do it without the "label" property and only base it on color. Currently I have the following code

plt.subplot(121)
plt.gca().set_title('Female')
survived = female_data[:,1]
pclass = female_data[:,2]
age = female_data[:,5]
label_color = ['r' if i==0 else 'b' for i in survived]
axes = plt.gca()
axes.get_xaxis().set_visible(False)
plt.scatter(age, pclass, color=label_color)

Which render something like this, I want red and blue to have a single label in the plot. Any idea?

enter image description here

Mark Mikofski

plot with legend

https://colab.research.google.com/notebook#fileId=1RtdwW8ztZbBUpd67RWffKRVt0ae0dNkr

import matplotlib.pyplot as plt
import numpy as np

ages = np.array([1, 2, 1, 3, 1, 2, 2, 1, 1, 2])
pclass = np.arange(10)
survived = np.array([
    True, True, True, False, False,
    False, True, False, True, False
])
plt.plot(
    pclass[survived], ages[survived], 'bo',
    pclass[~survived], ages[~survived], 'ro'
)
plt.legend(['survived', 'died'])

Hopefully you can see my colab notebook or the image. Is this what you wanted? I don't think you want scatter.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Matplotlib: How to give color to scatterplot circles based on column values

Matplotlib add color legend with value based on another variable

How to scale legend elements down in a scatterplot matplotlib?

matplotlib scatterplot with legend

Matplotlib scatterplot legend

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

Matplotlib adding legend based on existing color series

matplotlib legend based only on line style but not color

How do I add color as a 3rd dimension to matplotlib scatterplot?

matplotlib - How to add two labels to one scatterplot

How to add legend to imshow() in matplotlib

How to color `matplotlib` scatterplot using a continuous value [`seaborn` color palettes?]

How to add legend labels per plotted column to multiple scatterplot subplots?

How to add ggplot legend to scatterplot with two separate dataset lines

Associate color with number in matplotlib scatterplot

Matplotlib Legend on Scatterplot from pandas dataframe

Scatterplot in matplotlib with legend and randomized point order

matplotlib legend background color

matplotlib: How to add legend of pixel value range?

How do I add a legend to a Matplotlib PolyCollection?

How to add markers on legend and graph - matplotlib

Matplotlib: How to add legend to scatter plot colors?

How to add a legend to matplotlib pie chart?

How to add legend below subplots in matplotlib?

How to add a legend to matplotlib scatter plot

Matplotlib how to add global legend for subplot of histograms

How to add a string as the artist in matplotlib legend?

Add background color to only one component of the legend in matplotlib

How to change the color of legend in chartjs and be able to add one more legend?