plot 4 plots on 3 axes

16 views (last 30 days)
Christoph Goettler
Christoph Goettler on 21 Sep 2020
Edited: Adam Danz on 13 Aug 2021
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;
data = readmatrix(file);
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;
addaxis(J{1}, V{1}, 'k');

Answers (2)

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

Adam Danz
Adam Danz on 21 Sep 2020
Edited: Adam Danz on 13 Aug 2021
If the x-values, F{1}, F{2}, V{1}, and Z{1}, are all on different scales that cannot be rescaled to share a single scale, 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 some methods which require you to scale variables so they can share an axis.
yyaxis method
Start with yyaxis where you can plot two variables of different y-scales along a shared x-axis. Scale F{1}, F{2}, V{1}, and Z{1} to share the same x-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')
Add multliple tick definitions
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 tick labels 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 the y or z-axis.
Overlay axes method
This method is a bit more clumsy, requires more memory, and also requires scaling. It can also be applied to yyaxis plots.
The idea is to create n different axes where n is the number of variables with different scales. The overlaid axes will share the same position with base axes 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
  4 Comments
Christoph Goettler
Christoph Goettler on 21 Sep 2020
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
Adam Danz on 21 Sep 2020
Edited: Adam Danz on 21 Sep 2020
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.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!