1

I am trying to loosely replicate the below colorbar in matplotlib:

enter image description here

I cannot seem to make the colorbar similar.

Here is what I have tried so far:

I defined the cvals & colors according to this post:

cvals  = [-30 , -25 , -20 , -15 , -10 , -7.5 , -5 , -2.5 , 0 , 2.5 , 5 , 7.5 , 10 , 15 , 20 , 25 , 30]
colors = ["#000080", "#000080" , "#3F00FF" , "#1F51FF" , "#0096FF" , "#6495ED" , "#00FFFF" , "white" , "white" , "white" , "#FAA0A0" , "#F88379" , "#FA8072" , "#FA5F55" , "#FF2400" , "#80461B" , "#7C3030"]

I then also according to the above post, I did the following:

norm=plt.Normalize(min(cvals),max(cvals))
tuples = list(zip(map(norm,cvals), colors))
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", tuples)

Then in the color bar I used

cb = fig.colorbar(
    boundaries = cvals ,
    spacing = 'uniform' ,
    ticks =  cvals , 
    drawedges = False
)

The colorbar looks like this:

enter image description here

I am not worried about the text being outside the bar or that the colors are not quite the same but I cannot seem to get the white in the right spots or make the bar display from -30 to 30. I am also worried the graph may not display the proper coloring based on the bins.

Any help would be appreciated.

kdbaseball8
  • 111
  • 8
  • Do you need a proper color bar (an object of type `colorbar`) or a set of correctly colored squares with correct text labels that was manually put together will be suitable for you? – Yulia V Nov 23 '22 at 23:14
  • 1
    To customize the color bar by yourself, use the following code See here in the reference for [details](https://matplotlib.org/stable/tutorials/colors/colorbar_only.html#sphx-glr-tutorials-colors-colorbar-only-py). `fig, ax = plt.subplots(figsize=(9, 1));fig.subplots_adjust(bottom=0.5);fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap), cax=ax, orientation='horizontal', boundaries=cvals, ticks=cvals, )` – r-beginners Nov 24 '22 at 03:46
  • Thank you, your answer gets me on the right path. – kdbaseball8 Nov 25 '22 at 15:02

0 Answers0