How can I put a custom colorbar into a figure with multiple datasets/datatypes

I have an interesting situation where I am trying to overlay 3 different datatypes and make a custom colorbar for one of them.
For context, it first has and RBG image loaded using imagesc, scaled to lat/lon limits. The colors of the image are supposed to correlate with concentration and this is what I which to display in the color bar (the dark green to bright yellow).
Secondly, it has two contours (contourf): one for the depth contours and one for the coastline. This is overlayed so that it lines up with the RGB image. These contours also have their own colormap
Finally, it has 3 sets of data plotted using just 'plot' overtop of that.
I have created a custom color set corresponding to the range of colors in the RGB image, but I need the land and everything else to follow a different colormap. I also need to somehow scale the colorbar output to the corresponding concentrations. Currently, the colorbar only uses the depth data, and changing the colormap changes the colormap of all data sets. I hope the images below make the issue more clear.
I would like the colorbar to have the colors of the first picture, but scaled to the concentrations and not depth. And I would like the rest of the image to look like the second picture.
Thank you in advance! And sorry if this isn't super clear.
Sean

4 Comments

Hi, can you share the code for better understanding?
Here is the code for the figure. I didn't upload it originally because its kind of complicated.
c_map = [0.2 0.1 0.0;0.25 0.15 0.0;0.3 0.2 0.05;0.35 0.25 0.1;0.45 0.3 0.1;0.5 0.35 0.1;0.55 0.4 0.1;0.6 0.45 0.1;
0.65 0.5 0.1;0.75 0.55 0.2;0.85 0.6 0.3;0.95 0.65 0.4;1 0.7 0.5;1 0.75 0.6;1 0.8 0.65;1 0.85 0.7;
0.2 0.2 0.2];
% c_map is originally for the depth contours and land mass. Now I am only
% using it for the land masses.
c_map2=[0.13 0.56 0.55; 0.19 0.71 0.47; 0.45 0.82 0.33; 0.51 0.83 0.30; 0.98 0.90 0.13];
% c_map2 corresponds to the colors of the green and yellow background
% image.
figure();
colormap(c_map) % c_map is the brown to black and then c_map2 is yellow to green
im=imagesc(lonlim_local,latlim_local_inv,A); % A is the RGB image for the green and yellow background
set(gca,'YDir','normal')
hold on
colorbar
% This is for the contour map of depth with labels.
contourf(m_lon,m_lat,m_bath',[0 -25 -50 -100 -200 -300], 'ShowText','on' ,"FaceAlpha",0);
hold on
% This is to show the land, starting at a depth of -0.5 m.
contourf(m_lon,m_lat,m_bath','LevelList',-0.5,'LineColor','black'); %
% This is to set the axis limits to a predefined latitude and longitude
% bounding box
ylim(latlim_local)
xlim(lonlim_local)
% The next three plots are to show the three colored line segments
hold on
r1=plot(shlw_rgn.("Lon (deg)"),shlw_rgn.("Lat (deg)"), 'LineWidth',5, "DisplayName","Near-shore Region");
hold on
r2=plot(zzg_rgn.("Lon (deg)"),zzg_rgn.("Lat (deg)"), 'LineWidth',5, "DisplayName","Off-shore Region");
hold on
r3=plot(rtrn_rgn.("Lon (deg)"),rtrn_rgn.("Lat (deg)"), 'LineWidth',5, "DisplayName","Return Region");
legend([r1 r2 r3]);
hold on
xlabel('Longitude (°)');
ylabel('Latitude (°)');
% This is to create the inset map with bounding box
axes('Position',[.15 .125 .15 .275])
box on
% Creates a contour of the north american coastline based at a depth of 0
contourf(i_lon,i_lat,i_bath','LevelList',0,'LineColor','black');
text(-82.5, 49, "Canada");
text(-87.5, 38, {"United", "States"});
hold on
% Shelf is the bounding box for the small box in the inset map
plot(shelf)
set(gca,'XTick',[], 'YTick', [])
I have tried, as shown in the following code, but when I do that the green and yellow RGB image doesn't show through the contour plot.
figure();
ax1=axes;
im=imagesc(ax1,lonlim_local,latlim_local_inv,A);
colormap(ax1,c_map2) % c_map is the brown to black and then c_map2 is yellow to green
set(gca,'YDir','normal')
colorbar(ax1)
hold on
ax2 = axes('position', ax1.Position);
contourf(ax2,m_lon,m_lat,m_bath',[0 -25 -50 -100 -200 -300], 'ShowText','on' ,"FaceAlpha",0);
colormap(ax2,c_map)
hold on
fontsize(16,"points")
contourf(ax2,m_lon,m_lat,m_bath','LevelList',-0.5,'LineColor','black'); %
ylim(latlim_local)
xlim(lonlim_local)
hold on
r1=plot(ax2,shlw_rgn.("Lon (deg)"),shlw_rgn.("Lat (deg)"), 'LineWidth',5, "DisplayName","Near-shore Region");
hold on
r2=plot(ax2,zzg_rgn.("Lon (deg)"),zzg_rgn.("Lat (deg)"), 'LineWidth',5, "DisplayName","Off-shore Region");
hold on
r3=plot(ax2,rtrn_rgn.("Lon (deg)"),rtrn_rgn.("Lat (deg)"), 'LineWidth',5, "DisplayName","Return Region");
legend([r1 r2 r3]);
hold on
xlabel('Longitude (°)');
ylabel('Latitude (°)');
ax3=axes('Position',[.15 .125 .15 .275]);
box on
contourf(ax3,i_lon,i_lat,i_bath','LevelList',0,'LineColor','black');
colormap(ax3,c_map)
text(-82.5, 49, "Canada");
text(-87.5, 38, {"United", "States"});
hold on
plot(shelf)
set(gca,'XTick',[], 'YTick', [])

Sign in to comment.

 Accepted Answer

I figured it out. I had to define axis targets as well as set the axis background color to 'none' and then align the axis limits.
Thanks!

More Answers (0)

Categories

Products

Release

R2023b

Community Treasure Hunt

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

Start Hunting!