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 = [ 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

Setting different color for each series in scatter plot

I don’t know what you mean by ‘manually’. You can choose a colourmap and make a colour array easily enough: import numpy as np import matplotlib.pyplot as plt import as cm x = np.arange(10) ys = [i+x+(i*x)**2 for i in range(10)] colors = cm.rainbow(np.linspace(0, 1, len(ys))) for y, c in zip(ys, colors): plt.scatter(x, y, … Read more

plot different color for different categorical levels

Imports and Sample DataFrame import matplotlib.pyplot as plt import pandas as pd import seaborn as sns # for sample data from matplotlib.lines import Line2D # for legend handle # DataFrame used for all options df = sns.load_dataset(‘diamonds’) carat cut color clarity depth table price x y z 0 0.23 Ideal E SI2 61.5 55.0 326 … Read more

How to color scatter markers as a function of a third variable

There’s no need to manually set the colors. Instead, specify a grayscale colormap… import numpy as np import matplotlib.pyplot as plt # Generate data… x = np.random.random(10) y = np.random.random(10) # Plot… plt.scatter(x, y, c=y, s=500) # s is a size of marker plt.gray() Or, if you’d prefer a wider range of colormaps, you … Read more

scatter plot in jfreechart from database

This complete example creates a suitable database table in memory, queries it into a JDBCXYDataset and displays the dataset in a scatter plot. Note how the first column becomes the domain, while successive columns become individual series. import java.awt.EventQueue; import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.util.Calendar; import java.util.Random; … Read more

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

Get data from plot with matplotlib

Using a slightly modified version of Joe Kington’s DataCursor: import matplotlib.pyplot as plt import matplotlib.mlab as mlab import matplotlib.cbook as cbook import numpy as np def fmt(x, y): return ‘x: {x:0.2f}\ny: {y:0.2f}’.format(x = x, y = y) class DataCursor(object): # “””A simple data cursor widget that displays the x,y location of a matplotlib artist … Read more