MATLAB Answers

How do i create a colour quiver plot (colquiver) on top of a pcolor depth map?

23 views (last 30 days)
Sanne van der Heijden
Sanne van der Heijden on 22 Sep 2020 at 19:57
Commented: Adam Danz on 22 Sep 2020 at 21:20
I want to create a figure with a depth map (using pcolor) with coloured vectors on top (using the function colquiver). Using only quiver this was easy but I cant mannage to fit the coloured vectors on top.
I tried to make 2 axes (because i want to use 2 colourmaps) but the colquiver function does nog alow 'ax' in the function. How can I combine the figures and use 2 colourmaps and have 2 colourbars?
The function colquiver and the figures are attached.
This is the script so far (not working and without my attempts) :
figure,
%depth map script
pcolor( x2, y2, depth2_rev'); hold on
shading flat; axis equal;
colormap(cmap.map{1,5});
c1 = colorbar; hold on;
caxis([-12 0]); hold on;
c1.Label.String = 'water depth (m)';
%colour vector plot
scale =1;
colquiver(quiver(x2(1:4:end),y2(1:4:end),U2Mean(1:4:end), V2Mean(1:4:end), scale),sqrt(U2Mean(1:4:end).^2+V2Mean(1:4:end).^2));
colormap(jet);
c2 = colorbar; hold on;
hold off, axis image
set(gca,'FontSize',15);
xlim([startx2 (startx2+(gridsize2*(xloc2-1)))]);
ylim([starty2 (starty2+(gridsize2*(yloc2-1)))]);
xlabel('x-UTM (km)');
ylabel('y-UTM (km)');

  0 Comments

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 22 Sep 2020 at 20:46
Edited: Adam Danz on 22 Sep 2020 at 20:48
"How can I ... use 2 colourmaps and have 2 colourbars?"
An axis can have more than 1 colorbar but it cannot have more than 1 colormap. A simple solution is to create a second, invisible axes for the sole purpose of hosting a second colormap while all of your data are plotted on the main, visible axis.
Here's a demo (see inline comments for details). I'm using the quiver function instead of the 3rd party colquiver function you mentioned.
% Add primary axes and assign a colormap for the pcolor data
figure()
ax = axes();
ax.Colormap = bone(255);
hold on
% Plot the pcolor data
[X,Y] = meshgrid(-2:0.2:2);
Z = X .* exp(-X.^2 - Y.^2);
h = pcolor(X,Y,Z);
h.EdgeColor = 'none';
% Add a 2nd invisible axis to host the 2nd colormap and colorbar
% HandleVisibility is set to off to avoid accidentally accessing those axes.
ax2 = axes('Visible','off','HandleVisibility','off');
% Define the colormap for the quiver data
cmap = hot(size(Z,1));
ax2.Colormap = cmap;
% Plot the quiver data
[U,V] = gradient(Z,0.2,0.2);
for i = 1:size(Z,1)
quiver(X(i,:),Y(i,:),U(i,:),V(i,:), .2, 'Color', cmap(i,:), 'LineWidth',2)
end
% Set properties for the main axes
axis equal
xlim([-2,2])
ylim([-2,2])
% Add the two colorbars and position the main axes so that
% both colorbars fit on the right side.
cb(1) = colorbar(ax);
axPos = ax.OuterPosition;
cb(2) = colorbar(ax2);
% Split the vertical space between the 2 CBs.
cb(2).Position(4) = cb(2).Position(4)*.48;
cb(1).Position(4) = cb(1).Position(4)*.48;
cb(1).Position(2) = sum(cb(2).Position([2,4]))+.04;
cb(2).Position([1,3]) = cb(1).Position([1,3]);
ax.OuterPosition = axPos;
% Set colorbar range
caxis(ax2, [-4,4])
% Label the colorbars
ylabel(cb(1),'pcolor')
ylabel(cb(2),'quiver')

More Answers (0)

Community Treasure Hunt

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

Start Hunting!