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

19 ビュー (過去 30 日間)
BeeTiaw 2021 年 5 月 12 日

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.

サインインしてコメントする。

### 採用された回答

DGM 2021 年 5 月 12 日
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
contourf(x,y,z,'edgecolor','none');
cb1 = colorbar;
h1 = gca;
h2 = axes;
% the other axes has a scatter plot with its own colorbar
scatter(xr,yr,100,zr,'filled','markeredgecolor','w')
set(h2,'color','none')
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(h1,'position',axpos,'xlim',xl,'ylim',yl);
set(h2,'position',axpos,'color','none','xlim',xl,'ylim',yl);
% set one colormap for each axes
% my display is inverted, so the colormaps are too
colormap(h1,1-ccmap)
colormap(h2,1-summer)
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.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by