1

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

enter image description here

Theo
  • 57,719
  • 8
  • 24
  • 41
cui liu
  • 11
  • 1

2 Answers2

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()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
0

I think you should use

MyCmap=colors.LinearSegmentedColormap.from_list("", list(zip(lvals,cvals)))

as in this solution (even if it is for a logarithmic scale there). Look at the end of the accepted answer.

FraWa
  • 63
  • 10