Clear Filters
Clear Filters

plot in two side of x-axis with common y-axis

4 views (last 30 days)
Hello community, I need help to write a code to plot as shown in figure. I could plot with negative value of x but i want to plot two different plot in a single plot inrespective sign of x-axis. i also want a common y-axis as shown in figure. it's fine if you could send me a simple sample code.
Thank you

Accepted Answer

Voss
Voss on 26 Jul 2023
% rudimentary data vaguely similar to what yours might be:
P = 0:3:15;
phi = [ ...
0.03 0.02 0.01; ...
0.016 0.01 0.006; ...
0.01 0.007 0; ...
0.007 0.005 0; ...
0.003 0 0; ...
0 0 0];
M = [ ...
9 9 9; ...
22 20 14; ...
27 18 0; ...
22 13 0; ...
17 0 0; ...
0 0 0];
% this tiledlayout will contain two axes side by side with no space between:
t = tiledlayout(1,2,'TileSpacing','none');
% create the two axes:
ax = [nexttile(t) nexttile(t)];
% reverse the x-direction of the left axes, set its y-axis
% location to the right, and turn off its box
set(ax(1),'XDir','reverse','YAxisLocation','right','Box','off')
% remove the y-tick labels and box of the right axes
set(ax(2),'YTickLabels',{},'Box','off')
% flip the order of the children, so that the yticklabels of the
% left axes will show up on top of the right axes
set(t,'Children',ax)
% hold
hold(ax,'on')
% plot stuff in the left axes
plot(ax(1),phi(:,1),P,'-k');
plot(ax(1),phi(:,2),P,'--k');
plot(ax(1),phi(:,3),P,':k');
% plot stuff in the right axes
plot(ax(2),M(:,1),P,'-k');
plot(ax(2),M(:,2),P,'--k');
plot(ax(2),M(:,3),P,':k');
% set the xlim and xticks
xlim(ax(1),[0 0.05])
xticks(ax(1),0.01*(1:5))
xlim(ax(2),[0 28])
xticks(ax(2),5.6*(1:5))
% make the axes have the same ylim
linkaxes(ax,'y')
% specify the ylim and yticks
ylim(ax,[0 18])
yticks(ax,(1:5)*3.6)
% add xlabels
xlabel(ax(1),'\phi (l/m)')
xlabel(ax(2),'M (kN-m)x10^3')
% make the title
title(t,'Px10^4')
  5 Comments
Voss
Voss on 26 Jul 2023
Oh, I think I see what you mean: that the x10^4 on the y-axis and the x10^3 on the right x-axis are actually multipliers for the ticks. In that case, something like this might be closer to what you want:
% rudimentary data vaguely similar to what yours might be:
P = (0:3:15)*1e4;
phi = [ ...
0.03 0.02 0.01; ...
0.016 0.01 0.006; ...
0.01 0.007 0; ...
0.007 0.005 0; ...
0.003 0 0; ...
0 0 0];
M = [ ...
9 9 9; ...
22 20 14; ...
27 18 0; ...
22 13 0; ...
17 0 0; ...
0 0 0]*1e3;
% this tiledlayout will contain two axes side by side with no space between:
t = tiledlayout(1,2,'TileSpacing','none');
% create the two axes:
ax = [nexttile(t) nexttile(t)];
% reverse the x-direction of the left axes, set its y-axis
% location to the right, and turn off its box
set(ax(1),'XDir','reverse','YAxisLocation','right','Box','off')
% remove the y-tick labels and box of the right axes
set(ax(2),'YTickLabels',{},'Box','off')
% flip the order of the children, so that the yticklabels of the
% left axes will show up on top of the right axes
set(t,'Children',ax)
% hold
hold(ax,'on')
% plot stuff in the left axes
plot(ax(1),phi(:,1),P,'-k');
plot(ax(1),phi(:,2),P,'--k');
plot(ax(1),phi(:,3),P,':k');
% plot stuff in the right axes
plot(ax(2),M(:,1),P,'-k');
plot(ax(2),M(:,2),P,'--k');
plot(ax(2),M(:,3),P,':k');
% set the xlim and xticks
xlim(ax(1),[0 0.05])
xticks(ax(1),0.01*(1:5))
xlim(ax(2),[0 28]*1e3)
xticks(ax(2),5.6e3*(1:5))
% make the axes have the same ylim
linkaxes(ax,'y')
% specify the ylim and yticks
ylim(ax,[0 18]*1e4)
yticks(ax,(1:5)*3.6e4)
% add xlabels
xlabel(ax(1),'\phi (l/m)')
xlabel(ax(2),'M (kN-m)')
% make the title
title(t,'P')

Sign in to comment.

More Answers (0)

Categories

Find more on Labels and Annotations 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!