![untitled.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/213404/untitled.png)
Plot line thickness changes based on value
129 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Emerson Butler
am 11 Apr. 2019
Kommentiert: Vignesh Radhakrishnan
am 29 Jan. 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.
0 Kommentare
Akzeptierte Antwort
Mark Sherstan
am 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
![untitled.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/213404/untitled.png)
3 Kommentare
Vignesh Radhakrishnan
am 29 Jan. 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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!