I am trying to generate a scatter plot of coordinates(x,y) where the color is decided by a value ranging between (-3,1.5). How do I plot all the points with value less than -1 with same color without them being omitted from the plot all together. I tried to use im.set_clim(-1,1.5)
, but it cutoff the values below -1 and didn't plot.
How do I do this?
A follow up question :
Is it possible to have sequential colormap where the values below -1 are all red (no gradient) and above -1 are having a sequential colormap which does not start with a shade of red.
vmin
and vmax
indicate which c-value corresponds to the lowest and highest colors of the color map. Default, all values lower than vmin
get the lowest color (dark purple for the 'viridis' map). You can change this color with cmap.set_under(...)
. The extend=
parameter shows the under color as a triangular arrow in the colorbar.
from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(0, 15, 50)
y = - np.sin(x) / (x + 1) * 8
num_colors = 10
cmap = plt.get_cmap('viridis', num_colors)
cmap.set_under('red')
plt.scatter(x, y, c=y, cmap=cmap, vmin=-1, vmax=1.5)
plt.colorbar(extend='min')
plt.show()