How to make colorbars symmetric?

28 visualizzazioni (ultimi 30 giorni)
Dirk
Dirk il 3 Giu 2013
Risposto: Walter Roberson il 1 Giu 2023
Hi
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!

Risposte (2)

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

Accedi per commentare.


Walter Roberson
Walter Roberson il 1 Giu 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);
subplot(1,2,1);
pcolor(X, Y, delta_2_top);
clim([minten, maxten]);
cb1 = colorbar();
cb1.Limits = [min(delta_2_top), max(delta_2_top)];
subplot(1,2,2);
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.

Categorie

Scopri di più su Colormaps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by