There are essentially two option on how to colorize scatter points.
1. External mapping
You may externally map values to color and supply a list/array of those colors to the scatter
's c
argument.
z = np.array([1,0,1,0,1])
colors = np.array(["black", "green"])
plt.scatter(x,y, c=colors[z])
2. Internal mapping
Apart from explicit colors, one can also supply a list/array of values which should be mapped to colors according to a normalization and a colormap.
- A
colormap
is a callable that takes float values between 0.
and 1.
as input and returns a RGB color.
- A normalization is a callable that takes any number as input and outputs another number, based on some previously set limits. The usual case of
Normalize
would provide a linear mapping of values between vmin
and vmax
to the range between 0.
and 1.
.
The natural way to obtain a color from some data is hence to chain the two,
cmap = plt.cm.Spectral
norm = plt.Normalize(vmin=4, vmax=5)
z = np.array([4,4,5,4,5])
plt.scatter(x,y, c = cmap(norm(z)))
Here the value of 4
would be mapped to 0
by the normalzation, and the value of 5
be mapped to 1
, such that the colormap provides the two outmost colors.
This process happens internally in scatter
if an array of numeric values is provided to c
.
A scatter
creates a PathCollection
, which subclasses ScalarMappable
. A ScalarMappable
consists of a colormap, a normalization and an array of values. Hence the above is internalized via
plt.scatter(x,y, c=z, norm=norm, cmap=cmap)
If the minimum and maximum data are to be used as limits for the normalization, you may leave that argument out.
plt.scatter(x,y, c=z, cmap=cmap)
This is the reason that the output in the question will always be purple and yellow dots, independent of the values provided to c
.
Coming back to the requirement of mapping an array of 0
and 1
to black and green color you may now look at the colormaps provided by matplotlib and look for a colormap which comprises black and green. E.g. the nipy_spectral
colormap
Here black is at the start of the colormap and green somewhere in the middle, say at 0.5
. One would hence need to set vmin
to 0, and vmax
, such that vmax*0.5 = 1
(with 1
the value to be mapped to green), i.e. vmax = 1./0.5 == 2
.
import matplotlib.pyplot as plt
import numpy as np
x,y = np.random.rand(2,6)
z = np.array([0,0,1,1,0,1])
plt.scatter(x,y, c = z,
norm = plt.Normalize(vmin=0, vmax=2),
cmap = "nipy_spectral")
plt.show()
Since there may not always be a colormap with the desired colors available and since it may not be straight forward to obtain the color positions from existing colormaps, an alternative is to create a new colormaps specifically for the desired purpose.
Here we might simply create a colormap of two colors black and green.
matplotlib.colors.ListedColormap(["black", "green"])
We would not need any normalization here, because we only have two values and can hence rely on automatic normalization.
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np
x,y = np.random.rand(2,6)
z = np.array([0,0,1,1,0,1])
plt.scatter(x,y, c = z, cmap = mcolors.ListedColormap(["black", "green"]))
plt.show()