[PYTHON] Continuously color with matplotlib scatter plot

A note on how to continuously color matplotlib scatter plots.

As a point

  1. Convert continuous real numbers to RGB continuous values using cmap
  2. Use the 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!

Code example

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)

image.png

Reference URL

--List of cmaps: https://matplotlib.org/examples/color/colormaps_reference.html

Recommended Posts

Continuously color with matplotlib scatter plot
2-axis plot with Matplotlib
3D plot with matplotlib
Create 3D scatter plot with SciPy + matplotlib (Python)
3D scatter plot with PyQtGraph
Stackable bar plot with matplotlib
Gradient color selection with matplotlib
[Python] How to draw a scatter plot with Matplotlib
Scatter plot
Lognormal probability plot with Python, matplotlib
How to plot a lot of legends by changing the color of the graph continuously with matplotlib
Animation with matplotlib
Japanese with matplotlib
Animation with matplotlib
Animate with matplotlib
Plot ROC Curve for Binary Classification with Matplotlib
How to display images continuously with matplotlib Note
Align Matplotlib graph colors with similar colors (color map)
(Memorandum) Make a 3D scatter plot with matplodlib
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
Heatmap with Python + matplotlib
Band graph with matplotlib
2D plot in matplotlib
Learn with Cheminformatics Matplotlib
Real-time drawing with matplotlib
Various colorbars with Matplotlib
Seaborn basics for beginners â‘¢ Scatter plot (jointplot) * With histogram
Time series plot / Matplotlib
Adjust axes with matplotlib
matplotlib Color list creation
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
Reformat the timeline of the pandas time series plot with matplotlib
Notes on coloring by value in the matplotlib scatter plot
Candle chart plot with plotly
Graph Excel data with matplotlib (1)
Try using matplotlib with PyCharm
Graph drawing method with matplotlib
matplotlib gallery and color map
Graph Excel data with matplotlib (2)
Animate multiple graphs with matplotlib