PLOTTING MULTIPLE Y AXES

11 Comments
Hi @Willian,
You can achieve these plots using the yyaxis function which will allow you to plot multiple datasets with different y-axes on the same graph. For more information on this function, please refer to
https://www.mathworks.com/help/matlab/ref/yyaxis.html
Below is my full code to help you achieve this:
% Read data from CSV file
T2022_1 = readtable('data.csv', 'VariableNamingRule', 'preserve');
% Combine Date and Time columns into a single datetime column
MyDateTime = T2022_1.Date + T2022_1.Time;
MyDateTime.Format = 'yyyy-MM-dd HH:mm:ss';
T2022_2 = [T2022_1(:,1) table(MyDateTime) T2022_1(:,3:end)];
% Create a figure for the plot
figure(1)
% Plot Pressure Separador (PIT) on the primary y-axis
yyaxis left
plot(T2022_2.MyDateTime, T2022_2.PIT, '-k', 'LineWidth', 1, 'DisplayName', 'Pressure Separador')
ylabel('Pressure - Psig')
% Add additional y-axes for other variables
yyaxis right
hold on
plot(T2022_2.MyDateTime, T2022_2.Millitm, '-b', 'LineWidth', 1, 'DisplayName', 'Millitm')
plot(T2022_2.MyDateTime, T2022_2.TIT, '-r', 'LineWidth', 1, 'DisplayName', 'TIT')
plot(T2022_2.MyDateTime, T2022_2.PD, '-g', 'LineWidth', 1, 'DisplayName', 'PD')
plot(T2022_2.MyDateTime, T2022_2.FIT, '-m', 'LineWidth', 1, 'DisplayName', 'FIT')
% Customize the plot
grid on
xlabel('Date & Time')
lgd = legend;
lgd.NumColumns = 1;
So, I used the above mentioned function yyaxis left to set the primary y-axis for the 'PIT' variable. Additional y-axes are added using yyaxis right for 'Millitm', 'TIT', 'PD', and 'FIT'. also, each dataset is plotted with a different color and line style for clarity and the legend is included to differentiate between the plotted variables.
Please see attached plots along with snippet code.


Hope, this answers your question.
Accepted Answer
More Answers (0)
Categories
Find more on JSON Format in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!