how to overlay these two data sets in one image with two different colorbars?
10 views (last 30 days)
Show older comments
I am trying to overlay the two matrices images ( lol-blackjet) and (lol9-hot), so the target is to create one image with two colorbars (blackjet and hot) where each corresponds to each of these matrices.
I tried this code, but it only plots the hot colorbar and shows transparency that I don't need. I want to see the "lol" in blackjet and the "lol9" in hot in the same plot (one image) with two bars one is east and one is west.
clc;
clear all;
close all;
load('lol10.mat');
load('lol9.mat');
load('xAx.mat');
load('zAx.mat');
load('AR.mat');
figoffset = 10;
figSize = 200;
% Load images
image1 = lol; % Assuming lol is the first matrix
image2 = lol9; % Assuming lol9 is the matrix
% Set up figure for overlay
fig = figure(8 + figoffset);
set(fig, 'Position', [40 + 3 * figSize, 40, figSize, figSize / AR]);
% Display the first image
h1 = imagesc(xAx, zAx, image1, [0 1]);
colormap(blackjet);
colorbar;
title('Mask + MaxP AE Env (r)', 'FontSize', 8);
% Hold on to overlay the second image
hold on;
% Display the second image with transparency
h2 = imagesc(xAx, zAx, image2, [0 1]);
colormap(hot);
colorbar;
title('Mask + MaxP AE Env (r)', 'FontSize', 8);
% % Set transparency (adjust alpha value)
alpha(h2, 0.5); % Adjust the transparency level
% Release the hold
hold off;
3 Comments
Matt J
on 12 Jan 2024
"blackjet" seems to have been omitted from the .mat files, see above.
Also, it would be helpful if you would combine your 5 .mat files into a single .mat file, so there is less for us to download.
Accepted Answer
DGM
on 12 Jan 2024
Edited: DGM
on 12 Jan 2024
I'm going to just put both colorbars on one side so that they don't get in the way of axis labels. It's just easier.
load('lol10.mat');
load('lol9.mat');
load('xAx.mat');
load('zAx.mat');
load('AR.mat');
figoffset = 10;
figSize = 200;
% Load images
image1 = lol; % Assuming lol is the first matrix
image2 = lol9; % Assuming lol9 is the matrix
% Set up figure for overlay
%fig = figure(8 + figoffset);
%set(fig, 'Position', [40 + 3 * figSize, 40, figSize, figSize / AR]);
% Display the first image
imagesc(xAx, zAx, image1, [0 1]);
cb1 = colorbar;
h1 = gca;
h2 = axes;
% Display the second image with transparency
imagesc(xAx, zAx, image2, [0 1]);
title('Mask + MaxP AE Env (r)', 'FontSize', 8);
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
colormap(h1,blackjet(256))
colormap(h2,hot(256))
% Set transparency (adjust alpha value)
alpha(h2, 0.5); % Adjust the transparency level
Bear in mind that overlaying pcolor plots with transparency basically makes them unreadable.
More Answers (1)
Hassaan
on 12 Jan 2024
clc;
clear all;
close all;
load('lol10.mat');
load('lol9.mat');
load('xAx.mat');
load('zAx.mat');
load('AR.mat');
figoffset = 10;
figSize = 200;
% Load images
image1 = lol; % Assuming lol is the first image
image2 = lol9; % Assuming lol9 is the second image
% Set up figure for overlay
fig = figure(8 + figoffset);
set(fig, 'Position', [40 + 3 * figSize, 40, figSize, figSize / AR]);
% Create two subplots within the same figure
subplot(1, 2, 1);
h1 = imagesc(xAx, zAx, image1, [0 1]);
colormap(gca, blackjet);
colorbar;
title('lol (blackjet)', 'FontSize', 8);
subplot(1, 2, 2);
h2 = imagesc(xAx, zAx, image2, [0 1]);
colormap(gca, hot);
colorbar;
title('lol9 (hot)', 'FontSize', 8);
% Link the color limits of the colorbars for consistency
caxis([0 1]);
% Adjust the position of colorbars
pos1 = get(colorbar, 'Position');
pos2 = get(colorbar, 'Position');
pos1(1) = 0.92; % Adjust as needed
pos2(1) = 0.05; % Adjust as needed
set(colorbar, 'Position', pos1);
set(colorbar, 'Position', pos2);
% Adjust the size of colorbars if needed
% set(colorbar, 'Position', [x y width height]);
% Optionally, add a main title for the entire figure
sgtitle('Overlay of lol (blackjet) and lol9 (hot)', 'FontSize', 10);
% You can continue with any other customization you need
I created two subplots within the same figure, each displaying one of the images with its corresponding colormap and colorbar. We also link the color limits of the colorbars to ensure consistency in color mapping. You can adjust the positions and sizes of colorbars as needed for your specific layout preferences.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
See Also
Categories
Find more on Graphics Object Properties 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!