How To plot the space time diagram of an intersection for a traffic signal in matlab ?
7 views (last 30 days)
Show older comments
Mohamed Abdullah
on 30 Nov 2023
Commented: Mohamed Abdullah
on 4 Dec 2023
Hello , I am trying to plot a similar figure as in the attached photo . I am trying to plot the space time diagram for a traffic signal in an intersection。the figure illustrates the phase timing of a traffic signal that changes between red and green. the challenge is that the signal changes between the red and the green light and I have to make the width of each color match the time duration of the signal corresponding to it
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1556262/image.png)
0 Comments
Accepted Answer
Yatharth
on 4 Dec 2023
Edited: Yatharth
on 4 Dec 2023
Hi Mohamed,
I understand that you want to plot the space time diagram for a traffic signal in an intersection.
You can use MATLAB’s “rectangle” function to plot the rectangles of the exact width of the time duration of the signal.
Here is the link to the documentation for “rectangle” function. https://www.mathworks.com/help/matlab/ref/rectangle.html
I am attaching two examples combining both you can achieve your desired output.
Example 1 Multiple Intersections on Y axis
% Define signal phase durations for each intersection
redDuration = 20; % in seconds
greenDuration = 40; % in seconds
% Create a figure
figure;
% Define the intersection names
intersections = {'Intersection 1', 'Intersection 2', 'Intersection 3', 'Intersection 4'};
w = 0.2;
h = w/2;
% Plot the red and green signal phases for each intersection
for i = 1:4
% Plot the red signal phase
rectangle('Position', [0, i-h, redDuration, w], 'FaceColor', 'r');
hold on;
% Plot the green signal phase
rectangle('Position', [redDuration, i-h, greenDuration, w], 'FaceColor', 'g');
hold on;
end
% Customize the plot
xlabel('Time (seconds)');
ylabel('Intersection');
title('Traffic Signal Space-Time Diagram');
ylim([0.5 4.5]);
yticks([1 2 3 4]);
yticklabels(intersections);
grid on
Note: Here you can adjust the variables "redDuration" , "greenDuration" according to your use case/ algorithm for each intersection.
Example 2 Multiple Signals on X axis
% Define signal phase durations for each intersection
redDuration = 20; % in seconds
greenDuration = 30; % in seconds
% Create a figure
figure;
w = 0.2;
h = w/2;
% Plot the red signal phase for each intersection
for i = 1:4
rectangle('Position', [(i-1)*50, 0.5-h, redDuration, w], 'FaceColor', 'r');
hold on;
end
% Plot the green signal phase for each intersection
for i = 1:4
rectangle('Position', [(i-1)*50 + redDuration, 0.5-h, greenDuration, w], 'FaceColor', 'g');
hold on;
end
% Customize the plot
xlabel('Time (seconds)');
ylabel('Intersection');
title('Traffic Signal Space-Time Diagram');
ylim([0 5]);
yticks([0.5 1.5 2.5 3.5 4.5]);
yticklabels({'Intersection 1', 'Intersection 2', 'Intersection 3', 'Intersection 4'});
grid on
You can adjust the expression "(i-1)*50" and utilize your own specific values from an Array. By combining these two methods, you can generate the intended plot
I hope this helps!
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!