如何用不同的标记绘制多条线

问题描述 投票:0回答:7

我想用 MATLAB 绘制多条线并这样做,每条线的标记都会不同。我知道使用颜色可以通过

ColorSet = hsv(12);
来实现。有没有像这样简单的标记方法?

matlab
7个回答
10
投票

嗯,我不知道 MATLAB 的内置功能可以做到这一点,但我执行了以下操作。我创建自己的单元格:

markers = {'+','o','*','.','x','s','d','^','v','>','<','p','h'}

然后以这种方式访问它:

markers{mod(i,numel(markers))+1}

我还创建了一个函数

getMarker
,它可以执行此操作,并将其添加到 MATLAB 的路径中,以便我可以在所有脚本中访问它。


4
投票
x = linspace(0, 2*pi);
y = cos(bsxfun(@plus, x(1:15:end), x'));
figure
m = {'+','o','*','.','x','s','d','^','v','>','<','p','h'};
set(gca(), 'LineStyleOrder',m, 'ColorOrder',[0 0 0], 'NextPlot','replacechildren')
plot(x, y)

3
投票

是的,有一个现成的方法:它是 LineStyleOrder 轴属性。要激活它,您必须禁用 ColorOrder 属性,该属性优先于前者,并且默认情况下处于激活状态。您可以执行以下操作:

m = {'+','o','*','.','x','s','d','^','v','>','<','p','h'};
set_marker_order = @() set(gca(), ...
    'LineStyleOrder',m, 'ColorOrder',[0 0 0], ...
    'NextPlot','replacechildren');

其中

m
值是从
help plot
的输出手动获得的。 然后按照本例使用它:

x = linspace(0, 2*pi);
y = cos(bsxfun(@plus, x(1:15:end), x'));
figure
set_marker_order()
plot(x, y)

2
投票

以下内容也有帮助。

功能测试图

x=0:0.1:10;
y1=sin(x);
y2=cos(x);
m = ['h','o','*','.','x','s','d','^','v','>','<','p','h'];

plot(x,y1,[m(1)])
hold on;
plot(x,y2,[m(2)])

2
投票

我正在使用一个简单的程序来随机创建新的绘图样式。虽然它并不是真正的迭代,但有人可能会发现它有用:

function [styleString] = GetRandomLineStyleForPlot()
% This function creates the random style for your plot
% Colors iterate over all colors except for white one
  markers = {'+','o','*','.','x','s','d','^','v','>','<','p','h'};
  lineStyles = {'-', '--', ':', '-.'};
  colors = {'y', 'm', 'c', 'r', 'g', 'b', 'k'};
  styleString = strcat(markers(randi(length(markers), 1) ), ...
                lineStyles(randi(length(lineStyles), 1) ), ...
                colors(randi(length(colors), 1) ) );

end

1
投票

假设您使用的是

plot
,最简单的方法是在命令中添加行类型。 一些可能的选项是:
--
:
-
-.
。还有标记类型和宽度选项。

例如,此代码将生成具有不同类型标记的多行:

x = -pi:.1:pi;
y = sin(x);
z = cos(x);
t = tan(x);
l = x.^2;
figure();
hold on;
plot (x,y,'--g');
plot (x,z,'-.y');
plot (x,t,'-b');
plot (x,l,':r');
hold off;

生成的图是: The yellow line is hard to spot, but it's there

如需更多帮助,请访问:http://www.mathworks.com/help/techdoc/ref/linespec.html


0
投票

从 MATLAB R2024a 开始,您可以使用函数 linestyleorder 定义一组线条样式和标记。

例如,

Y = (10:15)'- (0:9);
plot(Y);
linestyleorder(["-", "--", "--x", ":."]);

给予

Figure with 10 lines, each with a different colour, and using various line styles and markers.


linestyleorder
还附带了一组预定义的样式。 例如,

Y = (10:15)'- (0:9);
plot(Y);
linestyleorder("mixedstyles");

给予

Figure with 10 lines, each with a different colour, and with different line patterns, but no markers.

Y = (10:15)'- (0:9);
plot(Y);
linestyleorder("mixedmarkers");

给予

Figure with 10 lines, each with a different colour, and with different markers, but not different line styles.

© www.soinside.com 2019 - 2024. All rights reserved.