Adding two shared colorbars to figures of tiledlayout
43 次查看(过去 30 天)
显示 更早的评论
Hi guys
I want to add two different colorbars to figures of tiledlayout function. I have searched the community and find something similar on https://www.mathworks.com/matlabcentral/answers/194554-how-can-i-use-and-display-two-different-colormaps-on-the-same-figure but it doesn't seem to work along with tiledlayout function. There are two main problems:
- The same colorbar is assigned to both data.
- I want two shared colorbars ploted on either side of the final tiledlayout figure, but only one of them remain.
The code along with necessary mat files as a zip file are attached.
Any help would be greatly appreciated.
clc, clear, close all
% load mat files.
load('StCoords.mat')
load('KGE_EachSt.mat')
load('x.mat')
load('y.mat')
load('Values.mat')
tiledlayout('flow')
PointsMinValue = min(KGE_EachSt(:));
PointsMaxValue = max(KGE_EachSt(:));
DemMinValue = min(Values(:));
DemMaxValue = max(Values(:));
for i = 1:size(KGE_EachSt, 2)
nexttile
% Underlying layer.
imagesc(x, y, Values);
Dem = gca;
% Specifying colorbar limits.
caxis(Dem, [DemMinValue DemMaxValue])
% Specifying colorbar style.
colormap(Dem, 'cool')
axis xy
axis image
hold on
% Overlaying layer.
scatter(StCoords(:,2), StCoords(:,3), 20, KGE_EachSt(:,i), 'filled');
Points = gca;
% Specifying colorbar limits.
caxis(Points, [PointsMinValue PointsMaxValue])
% Specifying colorbar style.
colormap(Points, 'hot')
end
% Colorbar of points.
C = colorbar(Points);
C.Label.FontSize = 10;
C.Label.FontWeight = 'bold';
C.Layout.Tile = 'East';
% Colorbar of Dem.
C1 = colorbar(Dem);
C1.Label.FontSize = 10;
C1.Label.FontWeight = 'bold';
C1.Layout.Tile = 'West';
0 个评论
采纳的回答
Dave B
2021-8-25
编辑:Dave B
2021-8-25
You can only have on colorbar per axes: in your code you've set Points = gca and Dem = gca, but they're the same gca.
You can do this with separate axes:
x=linspace(0,10*pi,1000);
tiledlayout(1,2)
ax1=nexttile;
imagesc(sin(x')*cos(x))
ax2=nexttile;
imagesc(x)
colormap(ax2,'turbo')
cleft=colorbar(ax1);
cleft.Layout.Tile='west';
cright=colorbar(ax2);
cright.Layout.Tile='east';
As a workaround you can often accomplish mimic the effect with two overlaid axes and one of them invisible:
x=randn(1000,1);y=randn(1000,1);
t=tiledlayout(1,1);
ax1=axes(t);
histogram2(x,y,linspace(-3,3,30),linspace(-3,3,30),'DisplayStyle','tile')
colormap(ax1,'hot')
ax2=axes(t); %note that you cannot make two axes in the same tile with nexttile, but you can with axes
scatter(ax2,x,y,10,x.^2 + y.^2, 'filled')
colormap(ax2,'cool')
ax2.Visible='off';
linkaxes([ax1 ax2]);
linkprop([ax1 ax2],'CLim'); % linking the clims may not be appropriate in all cases...
c1=colorbar(ax1);
c1.Layout.Tile='west';
c2=colorbar(ax2);
c2.Layout.Tile='east';
4 个评论
Dave B
2021-8-25
@omid zandi I'm glad this wasn't too late to be helpful!
It's pretty easy to get the look you describe, you just needs to set the tile using the Layout.Tile property, but you'll maybe want to think about which axes the colors are linked with. In the code below I'll link all of the axes together so that there's no ambiguity
x=randn(100,1);y=randn(100,1);
t=tiledlayout(2,1);
ax1=axes(t);
histogram2(x,y,linspace(-3,3,10),linspace(-3,3,10),'DisplayStyle','tile')
colormap(ax1,'hot')
ax2=axes(t);
scatter(ax2,x,y,10,x.^2 + y.^2, 'filled')
colormap(ax2,'cool')
ax2.Visible='off';
x=randn(100,1);y=randn(100,1);
ax3=axes(t);
histogram2(x,y,linspace(-3,3,10),linspace(-3,3,10),'DisplayStyle','tile')
colormap(ax3,'hot')
ax3.Layout.Tile=2; % <--- key new line of code
ax4=axes(t);
scatter(ax4,x,y,10,x.^2 + y.^2, 'filled')
colormap(ax4,'cool')
ax4.Visible='off';
ax4.Layout.Tile=2; % <--- key new line of code
linkaxes(t.Children); % Adjusted the link commands to just use all of the axes in the layout
linkprop(t.Children, 'CLim');
c1=colorbar(ax1);
c1.Layout.Tile='west';
c2=colorbar(ax2);
c2.Layout.Tile='east';
t.OuterPosition(3)=.5;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Purple 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!