Adding legend based on existing color series

You can create the legend handles using an empty plot with the color based on the colormap and normalization of the scatter plot. import pandas as pd import numpy as np; np.random.seed(1) import matplotlib.pyplot as plt x = [np.random.normal(5,2, size=20), np.random.normal(10,1, size=20), np.random.normal(5,1, size=20), np.random.normal(10,1, size=20)] y = [np.random.normal(5,1, size=20), np.random.normal(5,1, size=20), np.random.normal(10,2, size=20), np.random.normal(10,2, … Read more

Multiple datasets on the same scatter plot

You need a reference to an Axes object to keep drawing on the same subplot. import matplotlib.pyplot as plt x = range(100) y = range(100,200) fig = plt.figure() ax1 = fig.add_subplot(111) ax1.scatter(x[:4], y[:4], s=10, c=”b”, marker=”s”, label=”first”) ax1.scatter(x[40:],y[40:], s=10, c=”r”, marker=”o”, label=”second”) plt.legend(loc=”upper left”) plt.show()

Multiple datasets on the same scatter plot

You need a reference to an Axes object to keep drawing on the same subplot. import matplotlib.pyplot as plt x = range(100) y = range(100,200) fig = plt.figure() ax1 = fig.add_subplot(111) ax1.scatter(x[:4], y[:4], s=10, c=”b”, marker=”s”, label=”first”) ax1.scatter(x[40:],y[40:], s=10, c=”r”, marker=”o”, label=”second”) plt.legend(loc=”upper left”) plt.show()

How to make a 3D scatter plot

You can use matplotlib for this. matplotlib has a mplot3d module that will do exactly what you want. import matplotlib.pyplot as plt import random fig = plt.figure(figsize=(12, 12)) ax = fig.add_subplot(projection=’3d’) sequence_containing_x_vals = list(range(0, 100)) sequence_containing_y_vals = list(range(0, 100)) sequence_containing_z_vals = list(range(0, 100)) random.shuffle(sequence_containing_x_vals) random.shuffle(sequence_containing_y_vals) random.shuffle(sequence_containing_z_vals) ax.scatter(sequence_containing_x_vals, sequence_containing_y_vals, sequence_containing_z_vals) plt.show() The code above generates a … Read more

How to plot a scatter plot with a legend label for each class

Actually both linked questions provide a way how to achieve the desired result. The easiest method is to create as many scatter plots as unique classes exist and give each a single color and legend entry. import matplotlib.pyplot as plt x=[1,2,3,4] y=[5,6,7,8] classes = [2,4,4,2] unique = list(set(classes)) colors = [plt.cm.jet(float(i)/max(unique)) for i in unique] … Read more

Color a scatter plot by Column Values

Imports and Data import numpy import pandas import matplotlib.pyplot as plt import seaborn as sns seaborn.set(style=”ticks”) numpy.random.seed(0) N = 37 _genders= [‘Female’, ‘Male’, ‘Non-binary’, ‘No Response’] df = pandas.DataFrame({ ‘Height (cm)’: numpy.random.uniform(low=130, high=200, size=N), ‘Weight (kg)’: numpy.random.uniform(low=30, high=100, size=N), ‘Gender’: numpy.random.choice(_genders, size=N) }) Update August 2021 With seaborn 0.11.0, it’s recommended to use new figure … Read more