How to make colorbars symmetric?

27 views (last 30 days)
Dirk on 3 Jun 2013
Answered: Walter Roberson on 1 Jun 2023
I've following problem;
I've two colorplots with tension values on a surface. Is there a possibility to make the colorbars symmetric?...with i.e. the green color on point 0 but with a different range i.e [-2000 1000] for the left and [-10000 0] for the right plot. So the scale should be the same.
Thanks in advance!

Answers (2)

Scott Hottovy
Scott Hottovy on 1 Jun 2023
You can grab the max data and center it using the following:
% Plot the data (for example using pcolor)
% Get color axis limits
caxis_limits = caxis;
% Adjust color axis limits
max_limit = max(abs(caxis_limits));
caxis([-max_limit max_limit]);
% Add colorbar
  1 Comment
Walter Roberson
Walter Roberson on 1 Jun 2023
side note:
Relatively recently, R2022a, MATLAB started to prefer clim instead of caxis()

Sign in to comment.

Walter Roberson
Walter Roberson on 1 Jun 2023
In order to have the color scale be consistent between the two, the axes CLim property must be the same for the two plots. In particular you would want min() of all of the data combined, to max() of all of the data combined as your axes CLim for both axes. You can use the clim convenience function or set the axes CLim property to establish the bounds.
Having done that, it is not necessary that the color bars show the same range. You can record the handles of the colorbar() objects, and you can set the Limits property of the colorbar object. The Limits property determines the portion of the color axes that is drawn in the colorbar, but does not affect the color scaling of the graphics objects.
combined_data = [delta_2_top(:); delta_3_top(:)];
minten = min(combined_data);
maxten = max(combined_data);
pcolor(X, Y, delta_2_top);
clim([minten, maxten]);
cb1 = colorbar();
cb1.Limits = [min(delta_2_top), max(delta_2_top)];
pcolor(X, Y, delta_3_top);
clim([minten, maxten]);
cb2 = colorbar();
cb2.Limits = [min(delta_3_top), max(delta_3_top)];
The green 0 point would occur roughly 2/3 of the way through for the first plot, and would occur at the right side for the second plot.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!