I want to add to my plot a colorbar
, which has a nonlinear scale. For example, for such a plot:
I would like to have just 5 different colors on the bar on the right-hand side, instead of the gradient (don't pay attention to the plot itself; it's just an example).
I don't want to use contourf
and would like to find some more general solution.
If you want to have discrete values in your colorbar, a quick way to do this would be to use the cmap=plt.cm.get_cmap()
function and pass the name of whatever colormap class you are working with, along with the desired number of bins.
import matplotlib.pyplot as plt
plt.style.use('classic')
%matplotlib inline
import numpy as np
# Random Data Visualation
x = np.linspace(0, 10, 1000)
data = np.sin(x) * np.cos(x[:, np.newaxis])
plt.imshow(data, cmap=plt.cm.get_cmap('viridis', 5))
plt.colorbar()
plt.clim(-1, 1);
More documentation on everything color maps in Matplotlib [here]