Plot one line with different linewidth according to a third array
1 回表示 (過去 30 日間)
古いコメントを表示
I would like to have a plot in which parts of the line will be of different LineWidth according to another array. I tried to do it with the following code:
x=[0:0.1:1]; y=sin(x);set = [0 0 0 1 1 0 0 1 1 0 0];
plot(x,y,'Color','b');hold;plot(x(set>0),y(set>0),'LineWidth',5,'color','b');
And I get this plot:

But as you see I wanted that there would be a middle part in which the line width is 1 instead of 5.
What is the correct way of doing this?
0 件のコメント
採用された回答
Adam Danz
2018 年 7 月 24 日
編集済み: Adam Danz
2018 年 7 月 24 日
That's because when you evaluate
x(set>0)
y(set>0)
you just get vectors which are interpreted as single lines
x(set>0)
ans =
0.3 0.4 0.7 0.8
y(set>0)
ans =
0.29552 0.38942 0.64422 0.71736
If I understand you correctly, you want the value in set(i) to determine the line width of line segment xy(i) to xy(i+1). So the length of 'set' should be 1 less than the length of x or y.
A simple way to achieve that would be to plot each segment in a loop. There's surely a non-loop method available that might sacrifice readability and simplicity.
figure
axh = axes;
hold(axh, 'on')
for i = 1:length(x)-1 %-1 because you're plotting by line segment
ph = plot(axh, x(i:i+1), y(i:i+1), 'color', 'b');
if set(i)==1
ph.LineWidth = 5;
end
end
4 件のコメント
Adam Danz
2018 年 7 月 25 日
The NaN replacement proposal above is a masking strategy.
There are other possibilities that are more complex. For example you could use shaded error bars and control the width of the line by the width of the error but you'll still have the problem of having more than one line since the error edges are independent lines. you could plot a surface instead of a line but then you're dealing with a three-dimensional object instead of two-dimensional. I'm not sure what else I could suggest.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!