Tried the midnorm and listed colormap, but do not know how to turn the front 0-20 into uniform multi-color (the picture shows the abbreviation, please do not mind), do not know how to draw the effect of the picture! ! Help
Asked
Active
Viewed 906 times
2 Answers
1
In this case it makes sense to use a BoundaryNorm
and a ListedColormap
. Define the N+1
bounaries for the N
colors of the colormap and use spacing="proportional"
in the colorbar call.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import BoundaryNorm, ListedColormap
cmap = ListedColormap(["black", "darkred", "crimson", "salmon", "navy", "violet", "yellow"])
bounds = [0,1,2,3,4,10,20,30]
norm = BoundaryNorm(bounds, cmap.N)
fig, ax = plt.subplots()
sc = ax.scatter(*np.random.rand(2,100), c=np.random.rand(100)*30, cmap=cmap, norm=norm)
fig.colorbar(sc, orientation="horizontal", spacing="proportional")
plt.show()

ImportanceOfBeingErnest
- 321,279
- 53
- 665
- 712