Plot line thickness changes based on value
192 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Mark Sherstan
2019-4-11
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
3 个评论
Vignesh Radhakrishnan
2024-1-29
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!