【问题标题】:How to plot a grid with different colors for certain points (MATLAB)如何为某些点绘制具有不同颜色的网格(MATLAB)
【发布时间】:2021-04-22 14:07:30
【问题描述】:

问题

我有一个正方形网格,尺寸为 NxN,网格点/节点之间的间距恒定。我想绘制这个网格,但有些点很特殊,希望它们以不同的颜色绘制。

期待

预期的情节应该是这样的,但有些块必须有不同的颜色!

代码

下面给出我荒谬而缓慢的解决方案:

N = 80;
x = 1:1:N;
y = 1:1:N;

rx = randi([1 80],1,1000); %represents the x coordinates of the special points 
ry = randi([1 80],1,1000); %represents the y coordinates of the special points 

[X,Y] = meshgrid(x,y);
Z = zeros(80,80);
figure(1)
surf(X,Y,Z);

%Abandon surf, use scatter instead
figure(2)
for i=1:N
    for j=1:N        
        plot(x(i),y(j),'bo');
        hold on
    end
end

for i=1:1000
    plot(rx(i),ry(i),'ro');
    hold on    
end
grid on    

因为我的眼睛在流血,正确的做法是什么?非常感谢。

【问题讨论】:

    标签: matlab plot grid


    【解决方案1】:

    颜色是由 Z 控制的,所以把 Z 设为不同的值。

    linear_indx=sub2ind([N,N],rx,ry);
    Z(linear_indx)=1;
    

    为一组不同的颜色更改颜色图(或制作您自己的颜色图)。或者使用surf(X,Y,Z,C)

    【讨论】:

    • 上帝。当然是被Z控制了。谢谢!
    • @Angelos 不用担心。公平地说,我认为 imagescimshow 对于这个例子来说是更好的选择,但我认为你的真实代码可能会更复杂一些
    猜你喜欢
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    相关资源
    最近更新 更多