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

1 次查看(过去 30 天)
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 个评论
simone ferrari
simone ferrari 2020-4-5
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 2020-4-6
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 个评论
simone ferrari
simone ferrari 2020-4-6
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

请先登录,再进行评论。

更多回答(1 个)

simone ferrari
simone ferrari 2020-4-5
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

类别

Help CenterFile Exchange 中查找有关 Blue 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by