如何调整组合图中各条线的线宽?



当我调整这种类型的图的线宽时,它可以工作。

plot(x1,y1, 'm','Linewidth',1)
hold on
plot(x2,y2, 'b','Linewidth',2)
hold on
plot(x3,y3, 'r','Linewidth',3)
hold on
plot(x4,y4, 'c','Linewidth',4)
hold on
plot(x5,y5, 'o','Linewidth',5)

但不是当我这样做的时候。

plot(x1, y1, 'm','Linewidth',1,x2, y2, 'b','Linewidth',2,x3, y3, 'r','Linewidth',3,x4, y4, 'c','Linewidth',4,x5, y5, 'o','Linewidth',5);

我收到一个错误。

是否可以调整组合图的线宽?

是否可以调整组合图的线宽?

不(嗯,是的,但不是以更清洁的方式(。

如果需要一行,可以一起调整所有这些参数。如果要控制它们中每个的外观,则需要进行单独的绘图。

您可以访问属性,但我怀疑这只是更长且不太清楚,因此不确定它是否真的是正确的解决方案。

h=plot(x1, y1, x2, y2, ...);
h(1).LineWidth=1;
h(2).LineWidth=2;
...

对于更多数量的绘图和/或额外属性更具可扩展性的替代方法使用arrayfun.基本上,如果您为数组中的所有绘图设置数据,则只需一行代码即可绘制所有数据

% set up the data and all plot attributes
x = {x1, x2, x3, x4, x5};
y = {y1, y2, y3, y4, y5};
styles = ['m', 'b', 'r', 'c', 'o'];
widths = [1, 2, 3, 4, 5];
% setup figure
figure
ax = axes('NextPlot','add'); % like calling hold on
% plot all elements
% equivalent to a for loop: for i = 1:length(x)
arrayfun(@(i) plot(ax, x{i}, y{i}, styles(i), 'linewidth', widths(i)), 1:length(x));

我不喜欢使用 hold,因为当你做下一个绘图时,很难知道它是打开还是关闭。 我喜欢在第一行使用情节,在后续行中使用情节,如下所示:

plot(x1, y1, 'm', 'linewidth', 1)
line(x2, y2, 'color', 'b', 'linewidth', 2)
line(x3, y3, 'color', 'r', 'linewidth', 3)
line(x4, y4, 'color', 'c', 'linewidth', 4)
line(x5, y5, 'color', 'o', 'linewidth', 5)

最新更新