如何根据值创建颜色图?
How to create a colormap based on value?
我有两个向量如下:
x = 0:5:50;
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20]
其中 x
表示 x 轴上的距离,sir_dB
表示 SNR。为此,我需要为 50 x 60m 的网格生成类似于此的颜色图:
基于 sir_dB
的值。
我尝试了以下方法:
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20];
xrange = 0:50;
yrange = -30:30;
% create candidate set
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1.
candidate_set = [X(:), Y(:)];
test_pt = [0 30];
radius = 5;
% find which of these are within the radius of selected point:
idx = rangesearch(candidate_set, test_pt, radius );
neighborhood = candidate_set(idx{1}, :);
一旦我有半径为 5m 的邻居,我需要根据对应的 x
值的 sir_dB
值为网格的那部分着色。
我需要以这样的方式绘制图,即对于所有大于 15 的 sir_dB
值,网格应为绿色,y
大于 0 时应为黄色,大于 0 时应为红色,y
大于-20.
有人可以告诉我如何做到最好吗?
我不确定你到底想要什么,但这应该能让你开始 contourf
。我增加了 xrange 和 yrange 的粒度,使半径更平滑,但如果你愿意,你可以改回来。
x = 0:5:50;
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20];
xrange = 0:0.1:50;
yrange = -30:0.1:30;
% create candidate set
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1.
candidate_set = [X(:), Y(:)];
test_pt = [0 30];
r = sqrt((test_pt(1)-X(:)).^2 + (test_pt(2)-Y(:)).^2);
idx = r>5;
snr = nan(size(X));
snr(idx) = interp1(x,sir_dB,X(idx),'linear');
% Some red, yellow, green colors
cmap = [0.8500 0.3250 0.0980;
0.9290 0.6940 0.1250;
0 0.7470 0.1245];
figure();
colormap(cmap);
contourf(X,Y,snr,[-20,0,15],'LineStyle','none');
在原始 sir_dB 旁边绘制等高线图,我们看到它排成一行(假设您想要线性插值)。如果您不想使用线性插值 'prev' 或 'next' 作为 interp1
方法。
figure();
colormap(cmap);
subplot(2,1,1);
contourf(X,Y,snr,[-20,0,15],'LineStyle','none');
subplot(2,1,2);
plot([0,50],[-20,-20],'-r',[0,50],[0,0],'-y',[0,50],[15,15],'-g',x,sir_dB);
这是另一个建议,为此使用 imagesc
。我用 % ->
:
忽略了下面代码中的更改
x = 0:5:50;
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20];
xrange = 0:50;
yrange = -30:30;
% create candidate set
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1.
% -> create a map for plotting
Signal_map = nan(size(Y));
candidate_set = [X(:), Y(:)];
test_pt = [10 20];
radius = 35;
% find which of these are within the radius of selected point:
idx = rangesearch(candidate_set,test_pt,radius);
neighborhood = candidate_set(idx{1}, :);
% -> calculate the distance form the test point:
D = pdist2(test_pt,neighborhood);
% -> convert the values to SNR color:
x_level = sum(x<D.',2);
x_level(x_level==0)=1;
ColorCode = sir_dB(x_level);
% -> apply the values to the map:
Signal_map(idx{1}) = ColorCode;
% -> plot the map:
imagesc(xrange,yrange,rot90(Signal_map,2))
axis xy
% -> apply custom color map for g-y-r:
cmap = [1 1 1 % white
1 0 0 % red
1 1 0 % yellow
0 1 0];% green
colormap(repelem(cmap,[1 20 15 35],1))
c = colorbar;
% -> scale the colorbar axis:
caxis([-21 50]);
c.Limits = [-20 50];
c.Label.String = 'SNR';
结果:
我有两个向量如下:
x = 0:5:50;
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20]
其中 x
表示 x 轴上的距离,sir_dB
表示 SNR。为此,我需要为 50 x 60m 的网格生成类似于此的颜色图:
基于 sir_dB
的值。
我尝试了以下方法:
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20];
xrange = 0:50;
yrange = -30:30;
% create candidate set
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1.
candidate_set = [X(:), Y(:)];
test_pt = [0 30];
radius = 5;
% find which of these are within the radius of selected point:
idx = rangesearch(candidate_set, test_pt, radius );
neighborhood = candidate_set(idx{1}, :);
一旦我有半径为 5m 的邻居,我需要根据对应的 x
值的 sir_dB
值为网格的那部分着色。
我需要以这样的方式绘制图,即对于所有大于 15 的 sir_dB
值,网格应为绿色,y
大于 0 时应为黄色,大于 0 时应为红色,y
大于-20.
有人可以告诉我如何做到最好吗?
我不确定你到底想要什么,但这应该能让你开始 contourf
。我增加了 xrange 和 yrange 的粒度,使半径更平滑,但如果你愿意,你可以改回来。
x = 0:5:50;
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20];
xrange = 0:0.1:50;
yrange = -30:0.1:30;
% create candidate set
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1.
candidate_set = [X(:), Y(:)];
test_pt = [0 30];
r = sqrt((test_pt(1)-X(:)).^2 + (test_pt(2)-Y(:)).^2);
idx = r>5;
snr = nan(size(X));
snr(idx) = interp1(x,sir_dB,X(idx),'linear');
% Some red, yellow, green colors
cmap = [0.8500 0.3250 0.0980;
0.9290 0.6940 0.1250;
0 0.7470 0.1245];
figure();
colormap(cmap);
contourf(X,Y,snr,[-20,0,15],'LineStyle','none');
在原始 sir_dB 旁边绘制等高线图,我们看到它排成一行(假设您想要线性插值)。如果您不想使用线性插值 'prev' 或 'next' 作为 interp1
方法。
figure();
colormap(cmap);
subplot(2,1,1);
contourf(X,Y,snr,[-20,0,15],'LineStyle','none');
subplot(2,1,2);
plot([0,50],[-20,-20],'-r',[0,50],[0,0],'-y',[0,50],[15,15],'-g',x,sir_dB);
这是另一个建议,为此使用 imagesc
。我用 % ->
:
x = 0:5:50;
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20];
xrange = 0:50;
yrange = -30:30;
% create candidate set
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1.
% -> create a map for plotting
Signal_map = nan(size(Y));
candidate_set = [X(:), Y(:)];
test_pt = [10 20];
radius = 35;
% find which of these are within the radius of selected point:
idx = rangesearch(candidate_set,test_pt,radius);
neighborhood = candidate_set(idx{1}, :);
% -> calculate the distance form the test point:
D = pdist2(test_pt,neighborhood);
% -> convert the values to SNR color:
x_level = sum(x<D.',2);
x_level(x_level==0)=1;
ColorCode = sir_dB(x_level);
% -> apply the values to the map:
Signal_map(idx{1}) = ColorCode;
% -> plot the map:
imagesc(xrange,yrange,rot90(Signal_map,2))
axis xy
% -> apply custom color map for g-y-r:
cmap = [1 1 1 % white
1 0 0 % red
1 1 0 % yellow
0 1 0];% green
colormap(repelem(cmap,[1 20 15 35],1))
c = colorbar;
% -> scale the colorbar axis:
caxis([-21 50]);
c.Limits = [-20 50];
c.Label.String = 'SNR';
结果: