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.
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.
Recommended Posts