Plot one line with different linewidth according to a third array

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?

 Accepted Answer

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 Comments

I actually tried this approach, but when I save the plot produced in this way as vector file the line is not saved as a single line, but as collection of many segments. So I wonder if there is a way to do it differently..
There's no way to assign different linewidth values to one line. If you're using lines and you want varying widths, you'll have to break the line up.
That being said, there are alternatives to my proposal above. For example, if 'set' is always 1s and 0s, you could add the thicker segments like this:
figure
axh = axes;
hold(axh, 'on')
plot(x,y, 'b-') %line #1
% Isolate thick coordinates
set2 = set | [0,set(1:end-1)];
x2 = x;
y2 = y;
x2(set2==0) = NaN;
y2(set2==0) = NaN;
% Plot thicker coordinates
plot(x2,y2, 'b-', 'LineWidth', 5) %line #2
That produces the same figure but now there are only 2 lines (thin one and thick one) instead of many segmented lines.
Thanks, this could be an option. But isn't there an option to use masked arrays in MATLAB? In Python there is the Numpy function masked_where , which can be used for the plot I'm describing here..
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.

Sign in to comment.

More Answers (0)

Products

Release

R2018a

Asked:

on 24 Jul 2018

Commented:

on 25 Jul 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!