How assign specific colours (different from the colourmap ones) to regions of a matrix

2 views (last 30 days)
Hi!
After plotting a 2D matrix with pcolor and a jet colormap, I need to give some specific colours (e.g, black, green and brown)
to some specific regions of the figure (without using fill, too long to define every vertex). Thanks!
  7 Comments
simone ferrari
simone ferrari on 5 Apr 2020
Yes Ameer, that is my problem, how to put the green, brown and blue patches of figure 2 into figure 1 manteining the colours.
Ameer Hamza
Ameer Hamza on 5 Apr 2020
Can you attach you dataset as a mat file. It will make it easy for me to suggest a solution.

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 6 Apr 2020
Try this code.
xplot_ph0 = loadmdm('xplot_ph0');
yplot_ph0 = loadmdm('yplot_ph0');
WindSpeed_ph0 = loadmdm('WindSpeed_ph0');
U_ph0 = loadmdm('FlowuSpeed_ph0');
V_ph0 = loadmdm('FlowvSpeed_ph0');
Map_ed_veg = loadmdm('Edifici_vegetazione_ph0');
Map_ed_veg_NaN = Map_ed_veg;
Map_ed_veg_NaN(Map_ed_veg==0) = NaN;
passo=30;
fig=300;
fig = fig + 1;
figure (fig);
WindSpeed_ph0 = 4*WindSpeed_ph0; % 4 is selected by trial and error, tune it as required
WindSpeed_ph0(~isnan(Map_ed_veg_NaN)) = Map_ed_veg_NaN(~isnan(Map_ed_veg_NaN));
pcolor(xplot_ph0,yplot_ph0,WindSpeed_ph0);
shading flat; axis square; colormap jet; caxis([0 20]); colorbar;
xlabel('X [m]'); ylabel('Y [m]');
hold on
q=quiver(xplot_ph0(1:passo:end), yplot_ph0(1:passo:end), U_ph0(1:passo:end), V_ph0(1:passo:end), 2);
q.Color='black';
hold off
fig = fig + 1;
figure(fig);
pcolor(xplot_ph0,yplot_ph0,Map_ed_veg_NaN);
shading flat;axis square; colormap jet; caxis([0 20]); colorbar;
xlabel('X [m]'); ylabel('Y [m]');
  1 Comment
simone ferrari
simone ferrari on 6 Apr 2020
Thank you Ameer, but this does not completely solve my issue, as:
  1. minor issue: the velocity values (field in the first image) are altered (but I can mix the images changing the colorbar);
  2. major issue: this allows to set one colour each time, but not to use colour different from the ones of the colormap.
Anyway, following your idea I have created a new colormap reassigned values according to figure 2: results are better but not completely satisfying. This is the code:
close all;
clear all;
xplot_ph0 = loadmdm('xplot_ph0');
yplot_ph0 = loadmdm('yplot_ph0');
WindSpeed_ph0 = loadmdm('WindSpeed_ph0');
U_ph0 = loadmdm('FlowuSpeed_ph0');
V_ph0 = loadmdm('FlowvSpeed_ph0');
Map_ed_veg = loadmdm('Edifici_vegetazione_ph0');
Map_ed_veg_NaN = Map_ed_veg;
Map_ed_veg_NaN(Map_ed_veg==0) = NaN;
mycolormap1=jet;
mycolormap1(1,:)=[0 0 0];
mycolormap2=mycolormap1;
mycolormap2(256,:)=[1 1 1];
passo=30;
fig=0;
fig = fig + 1;
figure(fig);
pcolor(xplot_ph0,yplot_ph0,Map_ed_veg_NaN);
shading flat;axis square; colormap(mycolormap1); colorbar; caxis([1 20]); colorbar;
xlabel('X [m]'); ylabel('Y [m]');
WindSpeed_ph0_b = WindSpeed_ph0;
WindSpeed_ph0_b(Map_ed_veg_NaN==1)=0;
WindSpeed_ph0_b(Map_ed_veg_NaN==11)=3;
WindSpeed_ph0_b(Map_ed_veg_NaN==15)=4.5;
fig = fig + 1;
figure (fig);
pcolor(xplot_ph0,yplot_ph0,WindSpeed_ph0_b);
shading flat; axis square; colormap(mycolormap1); colorbar;
xlabel('X [m]'); ylabel('Y [m]');
hold on
q=quiver(xplot_ph0(1:passo:end), yplot_ph0(1:passo:end), U_ph0(1:passo:end), V_ph0(1:passo:end), 2);
q.Color='k';
hold off
WindSpeed_ph0_b = WindSpeed_ph0;
WindSpeed_ph0_b(Map_ed_veg_NaN==1)=0;
WindSpeed_ph0_b(Map_ed_veg_NaN==11)=max(WindSpeed_ph0(:))+0.01;
WindSpeed_ph0_b(Map_ed_veg_NaN==15)=max(WindSpeed_ph0(:))+0.01;
fig = fig + 1;
figure (fig);
pcolor(xplot_ph0,yplot_ph0,WindSpeed_ph0_b);
shading flat; axis square; colormap(mycolormap2); colorbar;
xlabel('X [m]'); ylabel('Y [m]');
hold on
q=quiver(xplot_ph0(1:passo:end), yplot_ph0(1:passo:end), U_ph0(1:passo:end), V_ph0(1:passo:end), 2);
q.Color='k';
hold off

Sign in to comment.

More Answers (1)

simone ferrari
simone ferrari on 5 Apr 2020
Yes, sure!
In the .zip file you will find:
Example_Ameer.m (the main script)
loadmdm.m (a function to load matrices of matrices, as well as savemdm to save .mdm)
the .mdm files to generate the figures

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!