# plot 4 plots on 3 axes

32 views (last 30 days)
Christoph Goettler on 21 Sep 2020 at 17:23
Edited: Adam Danz on 21 Sep 2020 at 19:59
Hi everyone,
I am relatively new to matlab and am having lots of trouble plotting 4 plots on 3 axes. I have scoured the matlab community for a solution to this but I cannot seem to find one. Basically I need to plot F{1} vs J{1} and F{2} vs J{2} on the left y axis, I need to create a y axis on the right to plot V{1} vs J{1}, and then I need to create another y axis on the right to plot Z{1} vs J{1}. I have tried plotyyy and addaxis but cannot seem to get either to work. Below is my code, any and all help is greatly appreciated
clear all, close all, clc;
sheets = dir('*.xlsx');
J=cell(length(sheets),1); %time
F=cell(length(sheets),1); %COF
V=cell(length(sheets),1);%velocity
Z=cell(length(sheets),1);%force
for i=1:length(sheets);
file =sheets(i).name;
num2str(i);
t=data(:,1);
COF=data(:,52);
vel=data(:,105);
Force=data(:,156);
J{i}=t;
F{i}=COF;
V{i}=vel;
Z{i}=Force;
end
figure;
plot(J{1}, F{1}, J{2}, F{2});hold on;

Cris LaPierre on 21 Sep 2020 at 18:35
Start by looking into yyaxis.
As for adding a 3rd y-axis, see if this post helps.

Adam Danz on 21 Sep 2020 at 18:43
Edited: Adam Danz on 21 Sep 2020 at 19:59
If the x-values, F{1}, F{2}, V{1}, and Z{1}, are all on different scales that cannot be scaled to a single scaled, the plot is going to be a visual nightmare, difficult to read, and will have little to no utility. The whole point of a 2D plot is to see how y changes as a function of x and if there are 4 different x-scales, you're forcing the reader to interpret each function by visually isolating its data from the other data on the axes.
If those x-values can be scaled to share a common scale, do that. It will reduce your workload but more importantly, it will become a more useful visualization.
Here are two methods, both of which require you to scale some variables so they can share an axis.
yyaxis method
Start with yyaxis where you can plot two variables of different scales along a shared x-axis. Scale F{1}, F{2}, V{1}, and Z{1} to share the same scale. Since you want to plot pairs (F{1},J{1}), (F{2},J{2}), (V{1},J{1}), and (Z{1},J{1}), you can arrange the plot as seen below where x_F1, x_V1, X_Z1, and x_F2 are scaled x-values.
yyaxis left
hold on
plot(x_F1, J{1})
plot(x_V1, J{1})
plot(x_Z1, J{1})
plot(x4, J)
ylabel('J{1}')
yyaxis right
plot(x_F2, J{2})
ylabel('J{2}')
xlabel('x scaled')
This method can be faulty if some detail are ignored. It can also be applied to axes created by yyaxis.
The idea is to plot an initial pair of (x,y) data and then scale the rest of your y-values (or x-values) so that they are on the same scale as the intial data that are plotted. Then plot the scaled data on the same axis but add additional rows or columns of axis tick labels, one for each scale. With this method, all of your data are plotted on the same plot but your tick labels will have multiple definitions.
You'll need to compute the tick labels so that they correspond with the ticks. For example, if the y-ticks of the initial data are [5 10 15 20] and the second set of data are 2x the scale, then the 2nd column of ticks will be [10 20 30 40] for the same set of ticks.
Here are some examples applied to the x-axis but you can do the same with either y-axis.
Overlay axes method
This method is a bit more clumsy, requires the more memory, and also requires some scaling. It can also be applied to yyaxis plots.
The idea is to create n different axes where n is the number of comparisons you're making with variables from different scales. The axes will share the same position and their background color will be set to "none" so you can see through them. Use linkaxes and linkprop to ensure the axis position and limits always agree and set the ticks for each axis so they have the same number of ticks and the same spacing, otherwise you'll end up with a bunch of misaligned ticks without any way of knowing which tick goes with which axis. To do so, you'll need to set the xlim or ylim and compute the tick inteval for each axis.
Here are some examples

Show 1 older comment
Adam Danz on 21 Sep 2020 at 19:13
That simplifies things but it won't change the two suggestions in my methods. For example,
yyaxis left
hold on
plot(J{1},F{1})
plot(J{1},V{1})
plot(J{1},Z{1})
yyaxis right
plot(J{2},F{2})
assuming F, V, and Z are all within the same or very similar scales.
If you want to apply different y-tick-labels on the left side for the 3 variables, you can use the approaches in the first 3 links in the bullet point list in my answer -- all of which apply multiple y axis ticks on one side of a yyaxis plot.
Christoph Goettler on 21 Sep 2020 at 19:17
F, Z, and V are on very different scales so unfortunately it's not quite that simple but I will check out the links you shared and see if I can figure it out from there. Thank you!
Adam Danz on 21 Sep 2020 at 19:36
Ok, I just updated my answer to clarify the 2nd method and I renamed it. The "add multliple tick definitions" is a decent solution to needing more than 2 y-axes. The "overlay axes method" is another option but I recommend the prior one.

R2020a

### Community Treasure Hunt

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

Start Hunting!