[PYTHON] Notes on coloring by value in the matplotlib scatter plot

What is this article?

Scatter is used when drawing a scatter plot with matplotlib, but there are times when you want to color according to the value when a value is assigned to each point. I always forget how to draw, so this is a memo for that.

Source code

import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt

if __name__ == '__main__':
    N = 100
    X = np.random.rand(N, 2)
    y = np.random.rand(N) * 2 - 1
    sc = plt.scatter(X[:, 0], X[:, 1], vmin=-1, vmax=1, c=y, cmap=cm.seismic)
    plt.colorbar(sc)
    plt.show()

X represents a point in two dimensions, and y represents the value assigned to each point. This time, I assigned a value from -1 to 1 for each point. By specifying vmin and vmax in the arguments passed to plt.scatter, you can specify the minimum and maximum values of colormap. You can also draw the colorbar of the colormap by passing the return value of plt.scatter to plt.colorbar.

Execution result

demo.png

Recommended Posts

Notes on coloring by value in the matplotlib scatter plot
Notes on using matplotlib on the server
I want to change the color by clicking the scatter point in matplotlib
Label each point on the seaborn scatter plot
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
2D plot in matplotlib
Notes on transactions in the Java client library in the datastore
Translucent matplotlib legend
Notes on using matplotlib on the server
Jupyter, numpy, matplotlib notes used in reports
Notes on how to use marshmallow in the schema library
Notes on imshow () in OpenCV
[Python] Clustering results by K-means are reduced in dimension by PCA and plotted on a scatter plot.
Paste the matplotlib figure on excel
Continuously color with matplotlib scatter plot
Notes on installing Ubuntu 18.04 on the XPS 15 7590
The first step in Python Matplotlib
Save the audio data acquired by the browser in wav format on the server
Plot CSV of time series data with unixtime value in Python (matplotlib)
Notes on what to do when matplotlib scatter () / scatter3d () does not work
Specify the color in the matplotlib 2D map
Notes on nfc.ContactlessFrontend () for nfcpy in python
Notes on creating static files in Django
Jupyter, numpy, matplotlib notes used in reports
Read the file line by line in Python
Notes on using OpenCL on Linux on the RX6800
Read the file line by line in Python
Looking back on 2016 in the Crystal language
Display histogram / scatter plot on Jupyter Notebook
Notes on using code formatter in Python
The date is displayed incorrectly in matplotlib.
Define a division value in Django and easily reflect it on the screen
Get coordinate values and keyboard input values by clicking on the python / matplotlib diagram