plotting functions with two y axes

17 views (last 30 days)
Hello,
I have to plot two curves on the same graph, same x-axis and different y-axes (right and left). I can't draw the curves with different hatching, different thickness and same black color. I would also like to insert the title of the axes and the legend. Below is the attempt script (R2021b).
Many thanks for the help
figure(1)
x1 = 0:0.1:10;
y1=10+x1.*4;
x2 = 0:0.1:10;
y2=15*sin(x2/2)
t = tiledlayout(1,1);
ax1 = axes(t);
plot(ax1,x1,y1,'-k')
ax1.XColor = 'k';
ax1.YColor = 'k';
ax2 = axes(t);
plot(ax2,x2,y2,'--k')
ax1.XAxisLocation = 'bottom';
ax1.YAxisLocation = 'left';
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
grid

Accepted Answer

Star Strider
Star Strider on 19 Jan 2025
Edited: Star Strider on 19 Jan 2025
Use the yyaxis function. It should work with tiledlayout axes.
EDIT — (19 Jan 2025 at 16:10)
I am not certain what result you want, however your code otherwise works as posted.
Try this —
figure(1)
x1 = 0:0.1:10;
y1=10+x1.*4;
x2 = 0:0.1:10;
y2=15*sin(x2/2)
y2 = 1×101
0 0.7497 1.4975 2.2416 2.9800 3.7111 4.4328 5.1435 5.8413 6.5245 7.1914 7.8403 8.4696 9.0778 9.6633 10.2246 10.7603 11.2692 11.7499 12.2012 12.6221 13.0113 13.3681 13.6915 13.9806 14.2348 14.4534 14.6359 14.7817 14.8907
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
t = tiledlayout(1,1);
ax1 = axes(t);
hp(1) = plot(ax1,x1,y1,'-k', DisplayName="(x_1, y_1)");
ax1.XColor = 'k';
ax1.YColor = 'k';
ax2 = axes(t);
hp(2) = plot(ax2,x2,y2,'--k', DisplayName="(x_2, y_2)");
ax1.XAxisLocation = 'bottom';
ax1.YAxisLocation = 'left';
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
grid
xlabel(ax1, 'x_1, x_2')
ylabel(ax1, 'y_1')
ylabel(ax2, 'y_2')
title("Your Title Goes Here")
legend([hp], Location='best')
I made some minor changes, however I kept your original code.
.

More Answers (3)

Andrea
Andrea on 19 Jan 2025
dear Star Strider,
thank you very much, it works perfectly
Andrea

Andrea
Andrea on 22 Jan 2025
dear all,
I would like to plot multiple curves on the same graph using two y-axes. With your help I managed to plot two curves but I would need to add two more curves on the same axes, for a total of four curves.
I drew the third curve using the traditional “plot” command after the hold on command, if I add the equation of the fourth curve the scale of the y-axis changes compared to that of the current listing. I would like to keep the current scale.
Below is the equation of the curve that I would like to add: x4 = 0:0.1:0.6; y4 = -250.*x1+150. I would also like to add these two curves to the legend. I am adding the current listing.
figure(1) % new figure
x1 = 0:0.1:0.6;
y1 = -500.*x1+300;
x2 = 0:0.1:0.6;
y2 = 29.33.*x2;
x3 = 0:0.1:0.6;
y3 = 14.66.*x2;
x4 = 0:0.1:0.6;
y4 = -250.*x1+150;
t = tiledlayout(1,1);
ax1 = axes(t);
ax2 = axes(t);
hp(1) = plot(ax1,x1,y1,'--k', 'linewidth',1,DisplayName="(x_1, y_1)");
ax1.XColor = 'k';
ax1.YColor = 'k';
%ax3 = axes(t);
hp(2) = plot(ax2,x2,y2,'--k', 'linewidth',2,DisplayName="(x_2, y_2)");
% hp(3) = plot(ax2,x3,y3,'-k', 'linewidth',1,DisplayName="(x_3, y_3)");
% hp(4) = plot(ax2,x4,y4,'-k', 'linewidth',1,DisplayName="(x_4, y_4)");
ax1.XAxisLocation = 'bottom';
ax1.YAxisLocation = 'left';
%ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
% ax3.YAxisLocation = 'none';
% ax3.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
% ax3.Box = 'off';
grid
xlabel(ax1, 'x1')
ylabel(ax1, 'y1')
ylabel(ax2, 'y2')
%title("Your Title Goes Here")
legend([hp], Location='best')
%legend([hp])
hold on
plot(x3,y3,'-k', 'linewidth',2)
%plot(x4,y4,'-k', 'linewidth',2)
Thanks for your help!
Andrea
  1 Comment
Star Strider
Star Strider on 22 Jan 2025
It’s difficultt for me to figure out what you¹re doing. That aside, I cannot get the added curve (X4,y4) to work with it.
I went a compleetely different route and used the yyaxis function here —
figure(1) % new figure
x1 = 0:0.1:0.6;
y1 = -500.*x1+300;
x2 = 0:0.1:0.6;
y2 = 29.33.*x2;
x3 = 0:0.1:0.6;
y3 = 14.66.*x2;
x4 = 0:0.1:0.6;
y4 = -250.*x1+150;
% t = tiledlayout(1,1);
% ax1 = axes(t);
% ax2 = axes(t);
% hp(1) = plot(ax1,x1,y1,'--k', 'linewidth',1,DisplayName="(x_1, y_1)");
% % hp(3) = plot(ax1, x4, y4, '-.k', DisplayName='(X_4, y_4)');
% ax1.XColor = 'k';
% ax1.YColor = 'k';
% %ax3 = axes(t);
% hp(2) = plot(ax2,x2,y2,'--k', 'linewidth',2,DisplayName="(x_2, y_2)");
% % hp(3) = plot(ax2,x3,y3,'-k', 'linewidth',1,DisplayName="(x_3, y_3)");
% % hp(4) = plot(ax2,x4,y4,'-k', 'linewidth',1,DisplayName="(x_4, y_4)");
% hp(3) = plot(ax1, x4, y4, '-.k', DisplayName='(X_4, y_4)');
% ax1.XAxisLocation = 'bottom';
% ax1.YAxisLocation = 'left';
% %ax2.XAxisLocation = 'top';
% ax2.YAxisLocation = 'right';
% ax2.Color = 'none';
% % ax3.YAxisLocation = 'none';
% % ax3.Color = 'none';
% ax1.Box = 'off';
% ax2.Box = 'off';
% % ax3.Box = 'off';
% grid
% xlabel(ax1, 'x1')
% ylabel(ax1, 'y1')
% ylabel(ax2, 'y2')
% hp
% %title("Your Title Goes Here")
% % legend([hp], Location='best')
% % legend([hp])
% hold on
% plot(x3,y3,'-k', 'linewidth',2)
% %plot(x4,y4,'-k', 'linewidth',2)
figure
yyaxis left
hp(1) = plot(x1,y1,'--k', 'linewidth',1,DisplayName="(x_1, y_1)");
hold on
hp(2) = plot(x2,y2,'--k', 'linewidth',2,DisplayName="(x_2, y_2)");
hold off
yyaxis right
hp(3) = plot(x3,y3,'-k', 'linewidth',1,DisplayName="(x_3, y_3)");
hold on
hp(4) = plot(x4,y4,'-k', 'linewidth',1,DisplayName="(x_4, y_4)");
hold off
grid
legend([hp], Location='best')
Make appropriate changes to get your desired result.
.

Sign in to comment.


Andrea
Andrea on 23 Jan 2025
Dear Star Strider,
thank you very much for your help, by slightly modifying your script I got the graph I needed. I just can't write the names of the x, y1 and y2 axes. Here the script. Thank you!
Andrea
figure(2)
x1 = 0:0.1:0.6;
y1 = -500.*x1+300;
x2 = 0:0.1:0.6;
y2 = 29.8.*x2;
x3 = 0:0.1:0.6;
y3 = 14.9.*x2;
x4 = 0:0.1:0.6;
y4 = -250.*x1+150;
yyaxis left
hp(4) = plot(x4,y4,'-k', 'linewidth',1,DisplayName="F_1");
hold on
hp(1) = plot(x1,y1,'-k', 'linewidth',2,DisplayName="F_2");
hold on
%hold off
yyaxis right
hp(2) = plot(x2,y2,'--k', 'linewidth',2,DisplayName="P_1");
hp(3) = plot(x3,y3,'--k', 'linewidth',1,DisplayName="P_2");
%hold on
hold off
grid
legend([hp], Location='best')
  1 Comment
Star Strider
Star Strider on 23 Jan 2025
As always, my pleasure!
You can refer to the ylabel calls by using their appropriate axis references, however it is easiest to just put them in the appropriate parts of the code, as I do here.
Try this —
figure(2)
x1 = 0:0.1:0.6;
y1 = -500.*x1+300;
x2 = 0:0.1:0.6;
y2 = 29.8.*x2;
x3 = 0:0.1:0.6;
y3 = 14.9.*x2;
x4 = 0:0.1:0.6;
y4 = -250.*x1+150;
yyaxis left
hp(4) = plot(x4,y4,'-k', 'linewidth',1,DisplayName="F_1");
hold on
hp(1) = plot(x1,y1,'-k', 'linewidth',2,DisplayName="F_2");
hold on
%hold off
ylabel('Left Y-Axis', FontWeight='bold')
yyaxis right
hp(2) = plot(x2,y2,'--k', 'linewidth',2,DisplayName="P_1");
hp(3) = plot(x3,y3,'--k', 'linewidth',1,DisplayName="P_2");
%hold on
hold off
grid
ylabel('Right Y-Axis', FontWeight='bold')
% legend([hp], Location='best')
legend([hp], Location='N')
Also, since legend does not appear to be sensitive to the plots on both axes, I put it manually where it wiill not cover any parts of the llines.
.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!