A note on how to continuously color matplotlib scatter plots.
As a point
facecolors
and ʻedgecolors arguments provided by
plt.scatter () `
There are two.(20/01/24 10:34 postscript) I corrected the code example below by letting me know the easier way in the comments. Thank you @kochory!
We visualized the low-dimensional embedding of the data set Boston housing for the regression problem and the objective variable (property price in this example) with a scatter plot.
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston
from sklearn.manifold import TSNE
X, y = load_boston(return_X_y=True)
tsne = TSNE(n_components=2, random_state=0)
embX = tsne.fit_transform(X)
# 20/01/24 10:34 Corrected based on the comments received
plt.scatter(embX[:, 0], embX[:, 1], c=y, cmap='coolwarm')
#The following is the code before the addition
#Point 1:Convert y to an array of RGBA
#cm = plt.get_cmap('coolwarm') #Get matplotlib colormap
#normalized_y = (y -y.min()) / (y.max() - y.min()) #Objective variable y to 0~Normalize to 1
#color_levels = (normalized_y * 256).astype(int) #y further 0~Convert to 255
#colors = [cm(cl) for cl in color_levels] # cm(cl)Returns an RGBA tuple
#Point 2:Specify the argument of facecolors
#plt.scatter(embX[:, 0], embX[:, 1], s=10, facecolors=colors)
--List of cmaps: https://matplotlib.org/examples/color/colormaps_reference.html
Recommended Posts