Clear Filters
Clear Filters

setting color scale of two datasets in the same axis in Matlab

4 views (last 30 days)
I have two datasets on the same axis. One is plotted using "contourf" and the other one is using "scatter".
The one with scatter is "filled" according to the value of the Z-value.
The two datasets have different value range. How do I set the limit separately for the two datasets?
In the plot below, it seems the colormap is set according the latest dataset being plotted.
The "contour" will be plotted in the background while the filled circle (the second dataset) will be color-coded.

Accepted Answer

DGM on 12 May 2021
It's certainly possible. It's not any fun, if you ask me. You can do it with overlaid axes. This post contains one example:
I went for a less thorough approach:
x = linspace(0,1,100);
y = x.';
z = (x+y)/2;
nd = 20;
xr = rand(1,nd);
yr = rand(1,nd);
zr = rand(1,nd)*45-10;
% one axes has a contour plot and a colorbar
cb1 = colorbar;
h1 = gca;
h2 = axes;
% the other axes has a scatter plot with its own colorbar
cb2 = colorbar;
% store the position and limits because adjusting
% the colorbar will mess these up
axpos = get(h1,'position');
xl = get(h1,'xlim');
yl = get(h1,'ylim');
% adjust the colorbars so they don't overlap
barl = 0.48;
cb1.Position(2) = cb1.Position(2)+cb1.Position(4)*(1-barl);
cb1.Position(4) = cb1.Position(4)*barl;
cb2.Position(4) = cb2.Position(4)*barl;
% reassert axes geometry so they match
% set one colormap for each axes
% my display is inverted, so the colormaps are too
So now we have two different colormaps representing two different data ranges in two different plot types, all in the same figure. It's not the most robust setup. If I resize the window, the alignment gets messed up. You might try using linkaxes(), but I didn't.
You could try to stack the color bars side-by-side, but you'd have to do a lot more messing around with the object positions if you wanted that.

More Answers (0)


Find more on Colormaps in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!