如何根据值创建色彩图?

时间:2017-08-03 08:35:30

标签: matlab plot matlab-figure colormap

我有两个向量如下:

x = 0:5:50;
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20] 

其中x表示x轴上的距离,sir_dB表示SNR。为此,我需要为50 x 60米的网格生成一个颜色贴图,类似于:

enter image description here

基于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}, :);

一旦我拥有半径为5米的邻居,我需要根据相应sir_dB值的x值为网格的那一部分着色。

我需要以这样的方式绘制图表:对于大于15的sir_dB的所有值,网格应为绿色,黄色为y大于0,红色为{{1大于-20。

有人可以向我提供如何做到最好的建议吗?

2 个答案:

答案 0 :(得分:1)

我不确定你想要什么,但这应该让你开始使用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);

enter image description here

答案 1 :(得分:0)

Here is another suggestion, to use imagesc for that. I nothed the changes in the code below with % ->:

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';

The result:

enter image description here