Inconsistancy of colorbars across contourf() plots

1 view (last 30 days)
I am comparing pressure contours across several test cases, so I need for the levels, and the colors coresponding to each level to be consistant. Here is an example:
p1 = contourf(X, Y, Z,-3:0.5:1, "LineWidth", 0.2);
cb1 = colorbar("Direction","reverse");
ylim(cb1, [-3 1])
p2 = contourf(X, Y, Z,-3:0.5:1, "LineWidth", 0.2);
cb2 = colorbar("Direction","reverse");
ylim(cb2, [-3 1])
p3 = contourf(X, Y, Z, -3:0.5:1, "LineWidth", 0.2);
cb3 = colorbar("Direction","reverse");
ylim(cb3, [-3 1])
So I am not posting the complete code, but want to show how I am declairing the same levels for each contourf() and the same bounds for each colorbar. Here is an example of the figures I am generating:
So the levels are consistant, and the limits of the colorbar are consistant, but the mapping of color to value changes with each plot. How can I specify this correctly?
Thanks

Accepted Answer

Voss
Voss on 3 Dec 2023
Use clim() instead of setting the y-limits of the colorbars.
X = 1:10;
Y = 1:10;
Z1 = 4*rand(10)-3;
Z2 = 2*rand(10)-3;
Z3 = 2*rand(10)-1;
figure('Position',[10 10 1000 400])
tl = tiledlayout(1,3);
title(tl,'clim(_)','Interpreter','none');
nexttile
p1 = contourf(X, Y, Z1,-3:0.5:1, "LineWidth", 0.2);
cb1 = colorbar("Direction","reverse");
clim([-3 1])
nexttile
p2 = contourf(X, Y, Z2,-3:0.5:1, "LineWidth", 0.2);
cb2 = colorbar("Direction","reverse");
clim([-3 1])
nexttile
p3 = contourf(X, Y, Z3, -3:0.5:1, "LineWidth", 0.2);
cb3 = colorbar("Direction","reverse");
clim([-3 1])
figure('Position',[10 10 1000 400])
tl = tiledlayout(1,3);
title(tl,'ylim(cb,_)','Interpreter','none');
nexttile
p1 = contourf(X, Y, Z1,-3:0.5:1, "LineWidth", 0.2);
cb1 = colorbar("Direction","reverse");
ylim(cb1, [-3 1])
nexttile
p2 = contourf(X, Y, Z2,-3:0.5:1, "LineWidth", 0.2);
cb2 = colorbar("Direction","reverse");
ylim(cb2, [-3 1])
nexttile
p3 = contourf(X, Y, Z3, -3:0.5:1, "LineWidth", 0.2);
cb3 = colorbar("Direction","reverse");
ylim(cb3, [-3 1])

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!