How to add multiple scales on second y axis?

I want to combine 4 results with different scales and units. like the bottom picture, but with 2 left y and 2 right y axes. is there a way i can do this in matlab?

Respuestas (1)

Yusuf Suer Erdem
Yusuf Suer Erdem el 26 de Nov. de 2021
Hi Yasser, could you try these codes below? Good luck.
clc; clear; close all;
x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
x2 = 1:0.2:20;
y2 = x2.^2./x2.^3;
t = tiledlayout(1,1);
ax1 = axes(t);
plot(ax1,x1,y1,'-r')
ax1.XColor = 'r';
ax1.YColor = 'r';
ax2 = axes(t);
plot(ax2,x2,y2,'-k')
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';

8 comentarios

Yasser Mal
Yasser Mal el 27 de Nov. de 2021
Hi, thanks but the code doesn't work. Matlab doesn't recognise tiledlayout.
Yusuf Suer Erdem
Yusuf Suer Erdem el 27 de Nov. de 2021
which version of Matlab are you using?
Yasser Mal
Yasser Mal el 27 de Nov. de 2021
2018b
Could you use the codes like below? if not could you write the error message please?
clc; clear; close all;
x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
x2 = 1:0.2:20;
y2 = x2.^2./x2.^3;
t = tiledlayout(1,1);
t.TileSpacing = 'compact';
ax1 = axes(t);
plot(ax1,x1,y1,'-r')
ax1.XColor = 'r';
ax1.YColor = 'r';
ax2 = axes(t);
plot(ax2,x2,y2,'-k')
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
thanks again, but this doesn't work either. The error message:
Undefined function or variable 'tiledlayout'.
Error in Untitled (line 6)
t = tiledlayout(1,1);
Hi Yasser, I made a research and I found that 'tiledlayout' function is available only at versions Matlab 2019 and after. But you can do your job with these codes below;
clc; clear; close all;
x = linspace(0,10);
y = sin(3*x);
yyaxis left
plot(x,y)
z = sin(3*x).*exp(0.5*x);
yyaxis right
plot(x,z)
ylim([-150 150])
Yasser Mal
Yasser Mal el 28 de Nov. de 2021
I've been using this code and it's only capable of plotting two sets of data. thanks again
Not the right answer

Iniciar sesión para comentar.

Productos

Versión

R2018b

Etiquetas

Preguntada:

el 26 de Nov. de 2021

Comentada:

el 12 de Mayo de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by