Plotting three axis with one y axis

23 views (last 30 days)
Dolly More
Dolly More on 12 Aug 2021
Edited: darova on 14 Aug 2021
I am trying to plot 3 lines with different 3 X-axis and common Y axis.
1) I tried using addaxis but it is just adding 3 Y axis.
2. I also tried plots.m but it is showing some errors.
code:
Y=[depth];
x=[T15, S15, B15];
location = 'bottom';
labels = strjust(char('y-axis ','x=T ','x=S ','x=B '),'right');
% plotses(x,Y,location,labels);
[hl,he] = plots(x,Y,location,labels);
stylel = char('--','-.','-');
colorl = get(gcf,'DefaultAxesColorOrder');
for i = 1:size(x,2)
set(hl(i),'LineStyle',stylel(i,:),'Color',colorl(i,:))
set(he(i),'XColor',colorl(i,:))
end
Errors:
a) Error using * Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
b) Error in plots (line 80)
lim_new = lim_fix - [ori (ori-1)]*diff(lim_fix)*(n-1)*k/(1-(N-1)*k);
3. I also tried this LINK
It just gives me 3 X-axis and no 3 lines on the plots.
4. Different approach:
a=axes('units','normalized','position',[.1 .35 .7 .6],'xlim',[-2 2],'xtick',-2:1:2);
plots(M5_prf{3, 2}.B15, M1_5{1,2}, 'Color','r')
set(gca, 'YDir','reverse');
hold on
plots(M5_prf{2, 2}.S15, M1_5{1,2})
set(gca, 'YDir','reverse');
hold on
plots(M5_prf{1, 2}.T15, M1_5{1,2})
set(gca, 'YDir','reverse');
b=axes('units','normalized','position',[0.13 .06 0.775 0.000001],'xlim',[33 35],'color','none');
c=axes('units','normalized','position',[.13 .02 .775 0.000001],'xlim', [0.000006 0.000013],'color','none');
Last approach is giving me 3 curves with same color (attached image). I am unable to change their colors.If there is any better way to get 3 xaxis please let me know. Also I want to use 3axis plots for subplot/tiledlayout. Is it possible in matlab? If not which other software I can try?
Thanks!

Accepted Answer

Adam Danz
Adam Danz on 13 Aug 2021
Edited: Adam Danz on 13 Aug 2021
See this comment that lists links to many examples to follow.
But before proceeding, are you convinced that this is the best way to represent the data? Those are wildly different scales and it looks like viewers will have to perform mental gymnastics to match the data to the axes.
If the data are meant to be compared, why not normalize the x-values so all lines share the same normalized scale? If the data aren't meant to be compared, why not just use 3 different axes?

More Answers (1)

Kelly Kearney
Kelly Kearney on 13 Aug 2021
I like to do this sort of thing the manual way, using my offsetaxis function. It requires a little more manual coding than functions like plots, but it allows you to add as many x (or y) axes as you need:
depth = linspace(0,800,50);
T15 = rand(50,1)*3 - 1.5;
S15 = rand(50,1)*3 + 33;
B15 = rand(50,1)*7 + 6;
% Plot the 3 datasets to 3 overlapping axes, 1 line per axis
ax(1) = axes('position', [0.1 0.2 0.8 0.7]);
ax(2) = axes('position', ax(1).Position);
ax(3) = axes('position', ax(1).Position);
col = get(ax(1), 'DefaultAxesColorOrder');
ln(1) = plot(ax(1), T15, depth, 'color', col(1,:));
ln(2) = plot(ax(2), S15, depth, 'color', col(2,:));
ln(3) = plot(ax(3), B15, depth, 'color', col(3,:));
set(ax, 'ydir', 'reverse');
set(ax(2:3), 'color', 'none', 'ytick', []);
% Offset the x-axes for plots 2 and 3
hy2 = offsetaxis(ax(2), 'x', 0.1);
hy3 = offsetaxis(ax(3), 'x', 0.2);
set(ax(1), 'xcolor', col(1,:));
set(hy2, 'xcolor', col(2,:));
set(hy3, 'xcolor', col(3,:));

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!