Plt.scatter Cannot Recognize A Cmap Generated By Listedcolormap()
I am trying to customize a Colormap with ListedColormap() for a scatter plot. Here is a dataset for the scatter plot: labels = [ 0, 1, 1, 100, 100] X = np.array([[0, 2],
Solution 1:
Building upon @ImportanceOfBeingEarnest's comment, for your current values of labels
, you will get no yellow color if you consider the equally spaced range of 0-33.33, 33.34-66.66, 66.67-100. The following answer highlights this. The second figure below however shows the yellow color, provided you have the labels
falling between the correct range. Check the official page for more examples on BoundaryNorm
.
The key line here is ranges = np.linspace(labels.min(), labels.max(), len(color_list)+1)
which divides your range of values (labels
) into equally spaced intervals.
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
labels = np.array([ 0, 1, 1, 100, 100]) # converted to array for ease
X = np.array([[0, 2], [0, 0], [1, 0], [5, 0], [5, 2]])
color_list = ["red", "yellow", 'blue']
cmap = mpl.colors.ListedColormap(color_list)
ranges = np.linspace(labels.min(), labels.max(), len(color_list)+1)
norm = mpl.colors.BoundaryNorm(ranges, cmap.N)
plt.scatter(X[:,0], X[:,1], c=labels, cmap=cmap, s=200, norm=norm)
With yellow points
labels = np.array([ 0, 56, 63, 100, 100]) # <--- new label values
X = np.array([[0, 2], [0, 0], [1, 0], [5, 0], [5, 2]])
color_list = ["red", "yellow", 'blue']
cmap = mpl.colors.ListedColormap(color_list)
ranges = np.linspace(labels.min(), labels.max(), len(color_list)+1)
norm = mpl.colors.BoundaryNorm(ranges, cmap.N)
plt.scatter(X[:,0], X[:,1], c=labels, cmap=cmap, s=200, norm=norm)
Post a Comment for "Plt.scatter Cannot Recognize A Cmap Generated By Listedcolormap()"