PLOTTING MULTIPLE Y AXES

Good afternoon,
How could I create a graph with multiple y-axes: Millitm, PIT, TIT, PD, FIT
T2022_1 = readtable('data.csv', 'VariableNamingRule','preserve');
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])];
figure(1)
plot(T2022_2.MyDateTime, T2022_2.('PIT'), '-k', 'LineWidth',1, 'DisplayName', 'Pressure Separador')
grid on
xlabel('Date & Time')
ylabel('Pressure - Psig')
lgd = legend;
lgd.NumColumns = 1;
Similar like this plot
for the help, thank you in advance

11 Comments

DGM
DGM on 27 Jul 2024
Edited: DGM on 28 Jul 2024
You haven't mentioned it, but that image looks like someone using addaxis().
There are a number of forum threads on the subject.

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.

@Umar, please format posted code; very hard to read otherwise and can't use the COPY button to pick it for further use that way.
Also, being picky since yyaxis can only create two axes it doesn't really solve @Willian's request; there is no facility built into MATLAB HG that does it (why, only Mathworks knows), but to get the effect requested will have to use one of the File Exchange submittals or do from scratch...
@dpb, your comments are duly noted. However, it is a little difficult for me to format code using mobile device. Also, the copy button does not help much anyway. Regarding your query about using multiple yyaxis. My intention was to educate OP about function and help him understand concept of chart with two y-axes which is deviation from his comments but sharing knowledge is no harm. However, I would wait for OP’s response. Hope, with your help, the File Exchange submittal takes place and this issue gets resolved. Are there any alternatives to resolve it.
dpb
dpb on 27 Jul 2024
Edited: dpb on 27 Jul 2024
You can't select and Ctrl_E to format???
I don't use mobile devices much so haven't ever had the issue...that may be an underlying reason behind there being so much unformatted code any more. If that's the case, then Mathworks should figure out something for the interface to make it not so hard. But, it does make code much harder to read and I often take a poster's code as the starting point of responding so I find the copy function quite valuable/useful.
At present there are no builtin alternatives for more than a single y axis on each end of the main axes, no...
@dpb,
How you perform ‘Ctrl_E’ to format on a touch screen tablet, please advise.
“At present there are no builtin alternatives for more than a single y axis on each end of the main axes, no...”
I already understand that part but still thanks for explaining.
Yeah. I've tried to edit on mobile (I was using mobile above). It's maddeningly cumbersome and a lot of basic formatting is either unavailable (the code formatting) or difficult simply because it's difficult to even select the correct portion of the text.
FWIW, anyone who can do that much on mobile already has more patience than I do. I can't stand fumbling with touchscreen OSKs.
To format on mobile:
First, insert an empty line.
Next, paste in the code.
Now, after the code, insert an empty line.
Now, go back to the code, and insert two spaces at the beginning of each group of statements. A group of statements terminates each time there is a completely empty line. If I recall correctly, lines that have (only) (non-empty) whitespace do not terminate the group of statements.
dpb
dpb on 28 Jul 2024
Edited: dpb on 28 Jul 2024
"To format on mobile:"
Yeech! Not owning one other than the phone, I now know why I would never try more than a line or two if did.
@Umar -- it was a question....I honestly never think of anybody being on the mobile; it's all I can stand to do a short text message on the phone, what more try to write code. Old fogeys never change...even though I know people are hooked, it's just not in my space so I don't think of it.
@dpb,
Thank you for sharing your feedback regarding mobile formatting. I understand that writing code on a phone can be challenging, especially for those who are not accustomed to it. Your perspective as someone who primarily uses a phone for communication is valuable and help me consider different user experiences.
dpb
dpb on 28 Jul 2024
Edited: dpb on 28 Jul 2024
" Your perspective as someone who primarily uses a phone for communication ..."
More accurately, that is "as someone who uses a phone for only communication (and even then reluctantly)..." :)
In my former life I was 25+ years a single consultant tied to always having to be available. When I returned to the family farm and gave up the consulting gig, one prime life objective was to not be tied to a pager or other means of being at another's beck and call. The phone mostly resides on the buffet in the dining room unless I do on (the fairly rare) occasion have something for which I do want to stay in touch. But, that's just me and I recognize that any more that is an anachronism.
ADDENDUM
I had really noticed what seemed to me to be a marked decline in the percentage of postings in which code was being formatted but had never thought to tie that to the increasing use of mobile devices. I do think if this is the root or major cause (and given Walter's description of doing so I can see why almost nobody would/does) then Mathworks needs to seriously consider the user intrface for those devices to try to make it less cumbersome. Having essentially no experience in the area, I'm certainly in no position to indicate how they are to do that, but it does seem to be an issue that hampers the functionality of the forum for all, not just mobile users.
PS
"as someone who uses a phone for only communication ..."
I stand corrected. I do find the camera of value to take pictures of equipment as disassembling for repair, etc., and in the same arena the flashlight can be helpful in dark crannies as well...

Sign in to comment.

 Accepted Answer

Willian
Willian on 28 Jul 2024
Edited: Walter Roberson on 28 Jul 2024
Tks you for answers, I see that to create the plot with 4 or more y-axes requires time with the indicated functions.

More Answers (0)

Asked:

on 27 Jul 2024

Edited:

dpb
on 28 Jul 2024

Community Treasure Hunt

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

Start Hunting!