在Matlab中将颜色带添加到螺旋形状图中

时间:2013-03-17 16:34:26

标签: matlab colors scatter-plot

我有以下代码在 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');

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

1 个答案:

答案 0 :(得分:3)

你控制的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');

输出:

enter image description here

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

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;

输出:

enter image description here

写作噪音:你的补充太多了。 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');

您获得:

enter image description here

这是非常合理的。