How to solve a problem with the generation of multiple-colored segments on one line in Matlab plot
4 次查看(过去 30 天)
显示 更早的评论
Hello everyone, I am trying to plot a graph where there are colored segements on one line . The segements are colored based on a vector that has values 1,2 and 3. The vectror size is kind of big size 1X155879. The time length of the line is around 1600 (in sec) The problem is that at the begining, the code runs well for small vectors but for my current mentioned vector, when I run the code, it runs only at the first 150 points of time and then the simulation becomes very slow and the graph disappears and no output is shown. is this because the data vector is very large or not ? and how can I generate this graph properly for that big data vector (whether using my method or if there is a better way).
P.S: PC specs : Windows 11 Pro, 64Gb RAM, i7-14700K RTX4070
logic_outputs = {[1 2 3 2 1 3 2 2 2 1], [3 1 1 1 3 3 3 1 1 1],[2 1 3 2 3 2 3 1 1 1]};
%logic_outputs = {[randi([1, 3], 1,155879)]};
num_lines = length(logic_outputs);
% Create a time vector
time_vector = linspace(0, 1600, length(logic_outputs{1}) +1);
% Set the heights for each line
line_heights = [568, 780, 1500];
% Set axis limits and labels
xlim([0 1600]);
%ylim([-1 1]); % Adjust as needed
xlabel('Time (seconds)');
ylabel('Segment Status');
% Add colored segments based on logic output for each line
for line_index = 1:num_lines
logic_output = logic_outputs{line_index};
for i = 1:length(logic_output)
color = getColor(logic_output(i)); % Define getColor function based on your color-coding
plot([time_vector(i), time_vector(i+1)], [line_heights(line_index); line_heights(line_index)], 'Color', color, 'LineWidth', 10);
hold on
end
end
% Function to determine color based on logic output
function color = getColor(output)
switch output
case 1
color = [0, 1, 0]; % green (RGB values)
case 2
color = [1, 1, 0]; % yellow
case 3
color = [1, 0, 0]; % red
otherwise
color = [0, 0, 0]; % black (default)
end
end
0 个评论
采纳的回答
DGM
2024-3-10
编辑:DGM
2024-3-10
Yeah, you're going to have problems trying to create a bunch of line objects for all the segments. You'll probably have a better time doing it with patch() and using a color table.
% a cell array of floating-point integer vectors
logic_outputs = {[1 2 3 2 1 3 2 2 2 1], [3 1 1 1 3 3 3 1 1 1],[2 1 3 2 3 2 3 1 1 1]};
% the color table
CT = [0 1 0; 1 1 0; 1 0 0; 0 0 0];
% sizes of things
num_lines = numel(logic_outputs);
num_verts = numel(logic_outputs{1})+1; % assuming they're the same length
% Create a time vector
time_vector = linspace(0, 1600, num_verts);
% Set the heights for each line
line_heights = [568, 780, 1500];
% use patch() to create three colored lines
hold on
for k = 1:numel(logic_outputs)
patch([time_vector NaN], ...
[repmat(line_heights(k),[1 num_verts]) NaN], ...
[logic_outputs{k}([1:end end]) NaN], ...
'EdgeColor','flat','LineWidth',5,'cdatamapping','direct');
end
colormap(CT)
% Set axis limits and labels
xlim([0 1600]);
xlabel('Time (seconds)');
ylabel('Segment Status');
The third argument to patch() is the color data, given here as a vector of indices into the rows of CT. This index vector is expected to be integer-valued, but it's also class-sensitive. If the contents of logic_outputs are float class (e.g. 'double'), then indexing starts at 1, like everything else in MATLAB. If those vectors are instead integer-class (e.g. uint8), then indexing starts at 0 instead.
3 个评论
DGM
2024-3-10
编辑:DGM
2024-3-10
Line objects can only have one color, but a patch object can have color defined on a per-face or per-vertex basis.
In the abbreviated example you gave, we end up needing to create 3*10=30 graphics objects of class 'line'. If the vectors are longer, we'd create more objects. That can eat up a lot of resources fast.
In the comparable example based on patch(), we only create 3 graphics objects, each one representing an entire polyline. The number of high level graphics objects is independent of the vector lengths.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!