【问题标题】:Add Bands of colours to a spiral shape plot in Matlab在 Matlab 中将颜色带添加到螺旋形状图中
【发布时间】:2013-03-05 23:43:18
【问题描述】:

我有以下代码在 Matlab 上创建和绘制螺旋线。我想控制颜色并自己添加三到四种颜色,而不是 matlab 进行着色。我怎样才能自己设置颜色并控制颜色?这是我的代码:

N = 1000;
r = linspace(0,1,N);
t = (3*pi/2)*(1+2*r);
x(1,:) = t.*cos(t);
x(2,:) = t.*sin(t);
x(3,:) = zeros(1,N);
ms = 50;
%cm = colormap;
%cm(64,:);

figure;
scatter3(x(1,:), x(2,:), x(3,:), ms, t, 'filled');

有什么帮助吗?

@jucestain,着色工作,谢谢。我还有一个问题。如果像这样在上面的代码中添加噪音,结果会变得有趣,如图所示

    N = 1000;
    r = linspace(0,1,N);
    t = (3*pi/2)*(1+2*r);
    x(1,:) = t.*cos(t);
    x(2,:) = t.*sin(t);
    x(3,:) = zeros(1,N);
    % Set colors:
    t(1:250) = 1;
    t(251:500) = 2;
    t(501:750) = 3;
    t(751:1000) = 4;
    ms = 50;
    figure;
    scatter3(x(1,:), x(2,:), x(3,:), ms, t, 'filled');

    %add noise
    x(1,:) = x(1,:) + 5*randn(1,N);
    x(2,:) = x(2,:) + 5*randn(1,N);
    x(3,:) = x(3,:) + 5*randn(1,N);
    figure;
    scatter3(x(1,:), x(2,:), x(3,:), ms, t, 'filled');

这个情节的结果很笨拙,不知道为什么?我是在正确地添加噪音还是什么?剧情对吗?

【问题讨论】:

    标签: matlab colors scatter-plot


    【解决方案1】:

    您拥有的 t 向量控制颜色。您可以通过更改 t 中的值来控制它。这是一个例子:

    N = 1000;
    r = linspace(0,1,N);
    t = (3*pi/2)*(1+2*r);
    x(1,:) = t.*cos(t);
    x(2,:) = t.*sin(t);
    x(3,:) = zeros(1,N);
    % Set colors:
    t(1:250) = 1;
    t(251:500) = 2;
    t(501:750) = 3;
    t(751:1000) = 4;
    ms = 50;
    figure;
    scatter3(x(1,:), x(2,:), x(3,:), ms, t, 'filled');
    

    输出:

    以上内容取决于您使用的是哪种颜色图。如果你觉得它更简单,你也可以使用它:

    N = 1000;
    r = linspace(0,1,N);
    t = (3*pi/2)*(1+2*r);
    x(1,:) = t.*cos(t);
    x(2,:) = t.*sin(t);
    x(3,:) = zeros(1,N);
    % Set colors:
    ms = 50;
    figure;
    scatter3(x(1,1:250), x(2,1:250), x(3,1:250), ms, 'r', 'filled'); % red
    hold on;
    scatter3(x(1,251:500), x(2,251:500), x(3,251:500), ms, 'g', 'filled'); % green
    scatter3(x(1,501:750), x(2,501:750), x(3,501:750), ms, 'b', 'filled'); % blue
    scatter3(x(1,751:1000), x(2,751:1000), x(3,751:1000), ms, 'y', 'filled'); % yellow
    hold off;
    

    输出:

    WRT 噪音:你添加的太多了。 rand 的范围为 0 到 1,因此您添加的噪声范围为 0 到 5,这与数据中的值相当。如果你这样做:

    N = 1000;
    r = linspace(0,1,N);
    t = (3*pi/2)*(1+2*r);
    x(1,:) = t.*cos(t);
    x(2,:) = t.*sin(t);
    x(3,:) = zeros(1,N);
    % Set colors:
    t(1:250) = 1;
    t(251:500) = 2;
    t(501:750) = 3;
    t(751:1000) = 4;
    ms = 50;
    figure;
    scatter3(x(1,:), x(2,:), x(3,:), ms, t, 'filled');
    
    %add noise
    x(1,:) = x(1,:) + .5*randn(1,N);
    x(2,:) = x(2,:) + .5*randn(1,N);
    x(3,:) = x(3,:) + .5*randn(1,N);
    figure;
    scatter3(x(1,:), x(2,:), x(3,:), ms, t, 'filled');
    

    您获得:

    这很合理。

    【讨论】:

    • 这种着色有效,谢谢。我还有一个问题。如果像这样在上面的代码中添加噪音,结果会变得有趣,如图所示
    • @user2179716 如果答案有效,您应该接受它。另外,你能详细说明一下添加噪音吗?
    • 我只想通过使用均值为 0、方差为 5 的高斯随机数添加一些随机数来添加一些噪声。如我编辑的代码所示。
    • 是的,标准差小于1,是合理的,但是大于1,三一三、四、五等呢?
    • @user2179716 查看您的值范围,它们在 x 和 y 方向上的范围从 -10 到 10,在 x 方向上的范围更小 -0.5 到 0.5。这与scatter3 函数无关。您的信噪比太高了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    相关资源
    最近更新 更多