Can this plot be recreated in matlab?

2 views (last 30 days)
Ajmal Rasheeda Satheesh
Ajmal Rasheeda Satheesh on 3 May 2023
Edited: DGM on 4 May 2023
Can such a figure be recreated in matlab?, a pcolor plot for 2 different color matrices (both matrices being different 2d arrays) with a 2d colorbar?
Any help will be appreciated
  1 Comment
Les Beckham
Les Beckham on 4 May 2023
You will get better responses to your question if you provide sample data. Also, be specific about how you envision combining the "2 different color matrices". Are they to be stacked vertically, or what?

Sign in to comment.

Answers (1)

DGM
DGM on 4 May 2023
Edited: DGM on 4 May 2023
There are no built-in plotting tools that perform 2D colormapping. You'll have to do it yourself. See this thread.
That uses a predefined image as a map. If we want to recreate the given example with four linearly-blended colors, we could do that or we could choose a more direct approach. Consider the simple example:
% some fake data
% both arrays must have the same geometry
z = peaks(100);
datax = z/3;
datay = rot90(z)/3;
% display the data using 1D maps
subplot(1,2,1)
imagesc(datax);
set(gca,'ydir','normal')
colormap(gray)
caxis([-1 1].*max(caxis)) % force mapping to be zero-centered
colorbar('southoutside')
title('xdata')
subplot(1,2,2)
imagesc(datay);
set(gca,'ydir','normal')
colormap(gray)
caxis([-1 1].*max(caxis)) % force mapping to be zero-centered
colorbar('southoutside')
title('ydata')
Now that we have an idea of what the x,y data looks like, it's time to create the colormap and create the output image representing the data.
% generate a 2D colormap patch for use as a "colorbar" of sorts
% this does linear interpolation in sRGB, so expect it to be muddy
% both CT0 and CTpatch will be upside-down for correct mapping and presentation
mapw = 256; % width/height of map
CT0 = [1 1 0; 0 0 1; 1 0 0; 0 1 0]; % corner colors [NW; SW; NE; SE]
CT0 = flipud(reshape(ctflop(CT0),2,2,3)); % now map is a 2x2 RGB image
xq = linspace(1,2,mapw);
yq = xq.';
CTpatch = zeros(mapw,mapw,3);
for c = 1:3
CTpatch(:,:,c) = interp2(CT0(:,:,c),xq,yq,'bilinear');
end
% interpolate data to make pseudocolor image
% specify color axes extents for colormapping
climx = [-1 1];
climy = [-1 1];
% clamp data to map extrema
xq = imclamp(datax,climx);
yq = imclamp(datay,climy);
% interpolate
dataimage = zeros([size(datax) 3]);
for c = 1:3
dataimage(:,:,c) = interp2(climx,climy,CT0(:,:,c),xq,yq,'bilinear');
end
% plot the data
figure
subplot(1,2,1)
image(dataimage)
set(gca,'ydir','normal')
grid on
title('this is my data')
xlabel('longitude')
ylabel('latitude')
% plot a "colorbar"
subplot(1,2,2)
image(climx,climy,CTpatch)
set(gca,'ydir','normal')
grid on
xlabel('size')
ylabel('shape')
You may choose to resize or reposition the axes used for the "colorbar" patch, or to otherwise present it differently.
The only major difference between this example and the one I linked to is that the other example used the data to do interpolation in the full 2D colormap (i.e. CTpatch in this example), whereas in this example the data is used to do interpolation on the base 2x2 map (CT0) which was used to generate CTpatch. This simplifies some things since we had to do the same thing to generate CTpatch anyway, but the interpolation could have otherwise been done using CTpatch instead. The reference vectors (the first two arguments to interp2()) would just have to be generated from climx,climy as appropriate.

Categories

Find more on Geographic Plots in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!