MATLAB: I am trying to get the colorbar ticks to align with the color gradient

colorbargradientMATLABtick label spacingtick labels

Hello all, I have looked around on the community and have seen several colorbar posts but none of the examples (or questions) I saw have this problem.
Basically I want to have the colorbar tick labels line up with the changes in the color gradient. For example, the difference is most noticable at "0":
Notice the black ticks do not coincide with the changes in color. I clearly have 21 tick labels and therefore 20 spaces between each division, so I tried
colormap(parula(20));
But since it didn't line up I tried 21 and 19 but I cannot seem to get this to line up.
Any suggestions?
Thanks in advance.

Best Answer

  • This gives aligned ticks (at least in R2020a)
    ax = axes();
    ax.CLim = [-20 20];
    colormap(ax, parula(20));
    c = colorbar(ax);
    c.Ticks = -20:2:20;