How to easily plot a time series graph of logical quantities with 0-1 binary values as shown in the figure?如何方​便绘制如图所示的0-​1二值的逻辑量时间序​列图?

8 次查看(过去 30 天)
As shown in the figure, the horizontal axis is time, and each curve represents a variable, which is plotted as a thick solid rectangular line if it is 1, and a thin solid line if it is 0. I want to know how to draw this kind of diagram with the command of MATLAB? Thank You!
如图所示,横坐标为时间,每条曲线代表一个变量,若该变量为1则绘制成矩形粗实线,若为0则绘制为细实线。想问下这种图是用matlab的那个指令绘制的?要如何实现?

回答(1 个)

Ayush Aniket
Ayush Aniket 2024-2-21
Hi,
Assuming the time series data you want to plot to be in the format of the matrix where each row represents a variable over time, you can use a 'for' loop to iterate through the variables and another 'for' loop inside it to iterate through the time series data and plot with different thickness relative to the binary value of the data. Refer to the code snippet below:
% Define time series data
time = 1:10; % Example time points
% Define multiple variables (each row represents a variable over time)
variables = [
0 1 0 1 0 1 0 1 0 1;
1 0 1 0 1 0 1 0 1 0;
0 0 1 1 0 0 1 1 0 0;
% Add more variables as needed
];
variablesNames = ['var1'; 'var2'; 'var3'];
% Define line thicknesses
thinLine = 1;
thickLine = 3;
% Create the plot
figure;
hold on; % Hold on to plot multiple lines
% Loop through each variable
for i = 1:size(variables, 1)
% Extract the current variable's data
currentVar = variables(i, :);
% Loop through each time point
for t = 1:length(time) - 1
% Check the value of the variable at the current time point
if currentVar(t) == 0
% Plot a thin line segment
plot(time(t:t+1), [i i], 'b', 'LineWidth', thinLine);
else
% Plot a thick line segment
plot(time(t:t+1), [i i], 'b', 'LineWidth', thickLine);
end
end
end
% Set the y-axis limits to properly display all variables
ylim([0 size(variables, 1) + 1]);
% Set the y-axis ticks to label each variable
set(gca, 'YTick', 1:size(variables));
% Set the y-axis tick labels to the names of the variables
set(gca, 'YTickLabel', variablesNames);
% Label the axes
xlabel('Time');
ylabel('Variables');
% Turn off the hold
hold off;
For plotting a line segment with a specific thickness, we use the ''LineWidth' property of the 'plot' function. Refer to the following documentation link to read more about the function and the property:

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!