I want to 3 different plot on same figure with different axis?
16 views (last 30 days)
Show older comments
I want to 3 different plot on same figure with different axis?
Matlab code is attached and .mat file is also attached.
Let me know if we can plot 3 plot in one figure using different xaxis. And the final result should look like as in attached figure
clc;clear;
load("pcdeffect.mat");load("meffect.mat");load("ueffect.mat");
ratepcd = pcdeffect(1975,:);
x1 = [1:1:5];
y1 = ratepcd(1,[1,4,7,10,13]+1);
z1 = ratepcd(1,[1,4,7,10,13]+2);
figure(1)
set(gcf, 'Units', 'inches', 'Position', [2 2 15 5.5])
subplot(1,2,1)
hold on
plot(x1,y1,'d-r','Linewidth', 2, 'Markersize', 10);
set(gca, 'Fontname','Times New Roman','Fontsize', 20);
grid on; box on; ax = gca; ax.LineWidth=1;
ylabel('$y$','interpreter','latex','Fontname','Times New Roman','Fontsize', 20)
xlabel('$\epsilon_a$ (\%)','interpreter','latex','Fontname','Times New Roman','Fontsize', 20)
xticklabels({'Case 1','Case 2','Case 3','Case 4','Case 5' })
a=xlim;b=ylim;
xx = a(2)-a(1);yy = b(2)-b(1);
text(a(1)-1/8*xx,b(1)-1/8*yy,'(a)','interpreter','latex','Fontname','Times New Roman','FontSize',20);
subplot(1,2,2)
hold on
plot(x1,z1,'d-r','Linewidth', 2, 'Markersize', 10);
set(gca, 'Fontname','Times New Roman','Fontsize', 20);
grid on; box on; ax = gca; ax.LineWidth=1;
ylabel('$y$ ','interpreter','latex','Fontname','Times New Roman','Fontsize', 20)
xlabel('$\epsilon_a$ (\%)','interpreter','latex','Fontname','Times New Roman','Fontsize', 20)
xticklabels({'Case 1','Case 2','Case 3','Case 4','Case 5' })
a=xlim;b=ylim;
xx = a(2)-a(1);yy = b(2)-b(1);
text(a(1)-1/8*xx,b(1)-1/8*yy,'(b)','interpreter','latex','Fontname','Times New Roman','FontSize',20);
%
ratem = meffect(1974,:);
x1 = 0.49*[1,2,4,8,16];
y1 = ratem(1,[1,4,7,10,13]+1);
z1 = ratem(1,[1,4,7,10,13]+2);
figure(2)
set(gcf, 'Units', 'inches', 'Position', [2 2 15 5.5])
subplot(1,2,1)
hold on
plot(x1,y1,'o-g','Linewidth', 2, 'Markersize', 10);
set(gca, 'Fontname','Times New Roman','Fontsize', 20);
grid on; box on; ax = gca; ax.LineWidth=1;
ylabel('$y$','interpreter','latex','Fontname','Times New Roman','Fontsize', 20)
xlabel('$m$','interpreter','latex','Fontname','Times New Roman','Fontsize', 20)
a=xlim;b=ylim;
xx = a(2)-a(1);yy = b(2)-b(1);
text(a(1)-1/8*xx,b(1)-1/8*yy,'(a)','interpreter','latex','Fontname','Times New Roman','FontSize',20);
subplot(1,2,2)
hold on
plot(x1,z1,'o-g','Linewidth', 2, 'Markersize', 10);
set(gca, 'Fontname','Times New Roman','Fontsize', 20);
grid on; box on; ax = gca; ax.LineWidth=1;
ylabel('$y$ ','interpreter','latex','Fontname','Times New Roman','Fontsize', 20)
xlabel('$m$ ','interpreter','latex','Fontname','Times New Roman','Fontsize', 20)
a=xlim;b=ylim;
xx = a(2)-a(1);yy = b(2)-b(1);
text(a(1)-1/8*xx,b(1)-1/8*yy,'(b)','interpreter','latex','Fontname','Times New Roman','FontSize',20);
%
rateu = ueffect(1979,:);
x1 = 3.84*[1,2,4,8,16];
y1 = rateu(1,[1,4,7,10,13]+1);
z1 = rateu(1,[1,4,7,10,13]+2);
figure(3)
set(gcf, 'Units', 'inches', 'Position', [2 2 15 5.5])
subplot(1,2,1)
hold on
plot(x1,y1,'s-m','Linewidth', 2, 'Markersize', 10);
set(gca, 'Fontname','Times New Roman','Fontsize', 20);
grid on; box on; ax = gca; ax.LineWidth=1;
ylabel('$y$','interpreter','latex','Fontname','Times New Roman','Fontsize', 20)
xlabel('$u$','interpreter','latex','Fontname','Times New Roman','Fontsize', 20)
a=xlim;b=ylim;
xx = a(2)-a(1);yy = b(2)-b(1);
text(a(1)-1/8*xx,b(1)-1/8*yy,'(a)','interpreter','latex','Fontname','Times New Roman','FontSize',20);
subplot(1,2,2)
hold on
plot(x1,z1,'s-m','Linewidth', 2, 'Markersize', 10);
set(gca, 'Fontname','Times New Roman','Fontsize', 20);
grid on; box on; ax = gca; ax.LineWidth=1;
ylabel('$y$ ','interpreter','latex','Fontname','Times New Roman','Fontsize', 20)
xlabel('$u$ ','interpreter','latex','Fontname','Times New Roman','Fontsize', 20)
a=xlim;b=ylim;
xx = a(2)-a(1);yy = b(2)-b(1);
text(a(1)-1/8*xx,b(1)-1/8*yy,'(b)','interpreter','latex','Fontname','Times New Roman','FontSize',20);
2 Comments
Adam Danz
on 12 Feb 2023
> I want to 3 different plot on same figure with different axis?
TL;DR
Why are you using subplot(1,2,n) which only produces 2 axes?
Answers (2)
DGM
on 12 Feb 2023
Edited: DGM
on 12 Feb 2023
This is an example of two subplots using axisxx() from the link I posted earlier.
% figure setup
figure(1)
Panel1 = uipanel('Parent', gcf, 'BorderType', 'none');
Panel2 = uipanel('Parent', gcf, 'BorderType', 'none');
set(Panel1, 'Position', [0 0 0.5 1], 'Units', 'normalized');
set(Panel2, 'Position', [0.5 0 0.5 1], 'Units', 'normalized');
% create axes (the subplots)
ax1 = axes('Parent', Panel1);
ax2 = axes('Parent', Panel2);
% subplot 1 setup
axes(ax1);
y = linspace(0, 2*pi, 100);
x1 = 180 * y / pi;
x2 = sin(y);
x3 = cos(y);
hp = plot(x1,y,'k');
xx1 = axisxx(x2,y,'r-');
xx2 = axisxx(x3,y,'b-');
xdesc = {'y (degrees)','sin(y)','cos(y)'};
ylabel('y (radians)');
xlabel(xdesc{1});
xx1.XLabel = xdesc{2};
xx2.XLabel = xdesc{3};
legend(xdesc);
% subplot 2 setup
axes(ax2);
y = linspace(0, 2*pi, 100);
x1 = 180 * y / pi;
x2 = 2*sin(y);
x3 = 3*cos(y);
hp = plot(x1,y);
xx1 = axisxx(x2,y,'r--');
xx2 = axisxx(x3,y,'b:');
xdesc = {'y (degrees)','2*sin(y)','3*cos(y)'};
ylabel('y (radians)');
xlabel(xdesc{1});
xx1.XLabel = xdesc{2};
xx2.XLabel = xdesc{3};
legend(xdesc);
Again, note the comments on the FEX page. Axisxx() is unmaintained and the callbacks for dynamic resizing are broken. I've already commented them out in the attached files, but you'll have no dynamic resizing. Similarly, if you try to add a colorbar object, it will break.
Axisxx() handles subplots by creating them in uipanel() objects, so that's an extra complication. I don't see any way to manipulate whether the scale appears on the top or bottom, so I edited line 17 of axisxx.m to always place it on the bottom. Feel free to edit that.
There are probably other things that don't work.
0 Comments
Image Analyst
on 12 Feb 2023
"I want to 3 different plot on same figure with different axis? " Solution, use subplot(2,2,n) or nexttile
subplot(2, 2, 1);
plot(x1, y1, '-', 'LineWidth', 2);
subplot(2, 2, 2);
plot(x2, y2, '-', 'LineWidth', 2);
subplot(2, 2, 3);
plot(x3, y3, '-', 'LineWidth', 2);
" plot 3 plot in one figure using different xaxis." using same axes:
plot(x1, y1, '-', 'LineWidth', 2);
hold on;
plot(x2, y2, '-', 'LineWidth', 2);
legend('Data set 1', 'Data set 2');
5 Comments
DGM
on 12 Feb 2023
If you want to use unmaintained FEX tools, you have to be willing to fix them. Check the comments on the FEX page. If you want to use addaxis, you'll have to change that line to 'colororder' instead of the abbreviated 'colorord', which is no longer supported. I don't remember if that's the only instance that you'll need to edit.
That said, addaxis() can only add extra y-axis scales to the figure. It could be adapted to work on x-axis, but that's an entire project. The concept that addaxis() uses is to simply plot everything in the first axes object, but create dummy axes and manipulate their positions to create the extra scales.
There are some other old FEX tools that ostensibly do what you want:
... but that is also unmaintained and may require work as well.
If you only want two scales, then the existing tools would work:
... but you want three.
See Also
Categories
Find more on Line 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!