# Plot line thickness changes based on value

63 views (last 30 days)
Emerson Butler on 11 Apr 2019
Commented: Emerson Butler on 11 Apr 2019
Hello,
I'm trying to plot some boolean values, and to represent True (1) or False (0), I'd like to change the line thickness over time. If the value is false, the line should be thin, and if the value is true the line should be thick. I've been trying various methods, and most recently I've tried following this method: https://www.mathworks.com/videos/coloring-a-line-based-on-height-gradient-or-some-other-value-in-matlab-97128.html
This is what I've come up with, but which doesn't work:
%Logical Values
x = [1 1 0 0 1 0 1 1 1 0 0 0];
%c = desired Line width = 1 for False and 2 for True
c = x+1;
%t-axis values
t = 1:length(x);
%Just want a straight line, not dependent on x, only the line thickness
%should be dependent on x.
z = zeros(1,length(x));
plot(t,z,'LineWidth',c);
This results in an error saying c is invalid.
Another method I've tried which also doesn't work was taken from another similar question, found here:https://www.mathworks.com/matlabcentral/answers/1156-conditional-plotting-changing-color-of-line-based-on-value answered by Seth DeLand.
%Set Threshold
Level = 1;
%Determine indices where x is at the threshold
AtLevel = (x == Level);
%Create 2 lines of 0s
ThinLine = zeros(size(x));
ThickLine = zeros(size(x));
%Set values not to be plotted as NaN
ThinLine(AtLevel) = NaN;
ThickLine(AtLevel) = NaN;
plot(t,ThinLine,'b','LineWidth',1);
hold on;
plot(t,ThickLine,'b','LineWidth',2);
This doesn't give any errors, but also doesn't result in a continuous line.
This is just what I've tried, anything that will help me plot the values in the way I've described would be lovely.
Edit: I'd eventually be plotting multiple different boolean signals, and I'd like them to be on the same plot in a nice way. So one of the lines would be at 0, one at 1, one at 2, and so on. I'd probably do this by making the line as ones(size(x))*n, where n is some integer. That way I can visually represent a bunch of boolean signals as they change over time.

Mark Sherstan on 11 Apr 2019
Here is one way of doing it:
%Logical Values
x = [1 1 0 0 1 0 1 1 1 0 0 0];
t = 1:length(x);
% Create figure and line thickness
figure(1)
hold on
thick = 1
% Loop through logicals
for ii = 2:length(x)
if x(ii) == true
plot(t((ii-1):ii),x((ii-1):ii),'LineWidth',thick)
thick = thick + 1;
else
plot(t((ii-1):ii),x((ii-1):ii),'LineWidth',thick)
thick = thick - 1;
end
% Prevent line from dissapearing
if thick <= 1
thick = 1
end
end Emerson Butler on 11 Apr 2019
Great! I modified it to the following since my line thickness was greater than 2 in some areas:
x = [1 1 0 0 1 0 1 1 1 0 0 0];
t = 1:length(x);
z = zeros(1,length(x));
for ii = 2:length(x)
if x(ii) == true
plot(t((ii-1):ii),z((ii-1):ii),'b','LineWidth',2);
else
plot(t((ii-1):ii),z((ii-1):ii),'b','LineWidth',1);
end
end
Which resulted in this: Then I can add more logical signals in by doing something like
v = ones(1,length(x))*0.2;
w = ones(1,length(x))*0.4;
and plotting other boolean vectors along the lines 0.2 and 0.4
Edit: Just noticed that the line is thick from 1-2, should be thick from 1-2 (inclusive), then thin from 3 to 4 (inclusive), then thick for 5, thin for 6, thick for 7,8,9 (inclusive), then thin for the remainder, which is not what is happening in the above graph. Probably something with the indices.
Emerson Butler on 11 Apr 2019
Fixed the indices, so the new code is:
x = [1 1 0 0 1 0 1 1 1 0 0 0];
t = 1:length(x)+1;
z = zeros(1,length(x)+1);
figure(1);
hold on;
for m = 1:length(x)
if x(m) == true
plot(t((m):m+1),z((m):m+1),'b','LineWidth',3);
else
plot(t((m):m+1),z((m):m+1),'b','LineWidth',1);
end
end
Which results in: This is exactly what I wanted. A straight line where only the thickness is dependent on the value, and if the value is true for 1, the line is thick for all of 1, if it is true for 2, the line is thick for all of 2, if the value is false for 3, then the line is thin for all of 3, and so on. (e.g. index 1 = true results in 1 <= thick < 2 ). Thanks Mark for the guidance, when I had tried for loops to plot originally I was only trying to plot one point in the for loop which was resulting in an empty graph, instead of plotting from index:index+1.