plotting functions with two y axes
17 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Star Strider
on 19 Jan 2025
Edited: Star Strider
on 19 Jan 2025
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)
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.
.
0 Comments
More Answers (3)
Andrea
on 22 Jan 2025
1 Comment
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.
.
Andrea
on 23 Jan 2025
1 Comment
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.
.
See Also
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!

