Plot line thickness changes based on value

113 visualizaciones (últimos 30 días)
Emerson Butler
Emerson Butler el 11 de Abr. de 2019
Comentada: Vignesh Radhakrishnan el 29 de En. de 2024
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.

Respuesta aceptada

Mark Sherstan
Mark Sherstan el 11 de Abr. de 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
untitled.png
  3 comentarios
Emerson Butler
Emerson Butler el 11 de Abr. de 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:
Capture.PNG
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.
Vignesh Radhakrishnan
Vignesh Radhakrishnan el 29 de En. de 2024
Hi,
Thank you. I was searching around on how to do this. Do you know how to plot multiple lines at different levels and also change the y-axis labels? For example, state the line at 0 as Condition 1 and the line at 0,2 as condition 2 etc
Thanks,
Vignesh

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by