use*_*129 1 matlab plot matlab-figure
我想plot3随着数组索引的进展使用不同的颜色。
我有 3 个变量:x, y, z。所有这些变量都包含时间线进度中的值。
例如:
x = [1, 2, 3, 4, 5];
y = [1, 2, 3, 4, 5];
z = [1, 2, 3, 4, 5];
plot3(x, y, z, 'o')
Run Code Online (Sandbox Code Playgroud)
我希望看到颜色从 ( x(1), y(1), z(1)) 到图中最后一个值 ( x(5), y(5), z(5)) 的变化。如何动态改变这种颜色?
在我看来,这里的方法是使用scatter3,您可以在其中显式指定颜色值:
scatter3(x,y,z,3,1:numel(x))
Run Code Online (Sandbox Code Playgroud)
其中3是大小参数并1:numel(x)给出增加的颜色。之后您可以像往常一样选择颜色图。
使用plot3你可以做同样的事情,但它需要一个循环:
cMap = jet(numel(x)); % Generates colours on the desired map
figure
hold on % Force figure to stay open, rather than overwriting
for ii = 1:numel(x)
% Plot each point separately
plot3(x(ii), y(ii), z(ii), 'o', 'color',cMap(ii,:))
end
Run Code Online (Sandbox Code Playgroud)
我只会plot3在您想要元素之间连续彩色线条的情况下使用该选项,但这scatter3是不能做到的:
cMap = jet(numel(x)); % Generates colours on the desired map
figure
hold on % Force figure to stay open, rather than overwriting
for ii = 1:numel(x)-1
% Plot each line element separately
plot3(x(ii:ii+1), y(ii:ii+1), z(ii:ii+1), 'o-', 'color',cMap(ii,:))
end
% Redraw the last point to give it a separate colour as well
plot3(x(ii+1), y(ii+1), z(ii+1), 'o', 'color',cMap(ii+1,:))
Run Code Online (Sandbox Code Playgroud)
注意:在 R2007b 上测试并导出图像,与 R2021b 文档交叉检查语法