Line plot for 2 X-axis and 2 Y-axis in a single figure

4 ビュー (過去 30 日間)
M Fahd
M Fahd 2019 年 1 月 29 日
コメント済み: M Fahd 2019 年 1 月 30 日
Hi,
I am trying to plot X-Y graph(s) in a single figure with primary X-Y and secondary X-Y options. I have found (and editted) the following code for this purpose. However, it produces an offset in the secondary X & Y axis (see the attached figure arrows). Would really appreciate if someone can help me get rid of this issue. Thanks in advance.
clear
clc
close all
% Relevant to Primary X-Y axis
x1 = 0:.1:40;
y1 = 4.*cos(x1)./(x1+2);
y2 = 8.*cos(x1)./(x1+2);
% Relevant to Secondary X-Y axis
x2 = 1:.2:20;
y3 = x2.^2;
y4 = 1.25*(x2.^2);
% Plots in Primary X and Y axis
hl1 = line(x1,y1,'Color','r');
hl2 = line(x1,y2,'Color','g');
ax1 = gca;
set(ax1,'XColor','k','YColor','k', 'TickDir','out')
axis square
xlabel ('Primary X_{axis} (unit)')
ylabel ('Primary Y_{axis} (unit)')
% Plots in Secondary X and Y axis
ax2 = axes('XAxisLocation','top', 'YAxisLocation','right', 'Color','none',...
'XColor','k','YColor','k', 'TickDir','out');
hl3 = line(x2,y3,'Color','k','Parent',ax2);
hl4 = line(x2,y4,'Color','b','Parent',ax2);
axis square
xlabel ('Secondary X_{axis} (unit)')
ylabel ('Secondary y_{axis} (unit)')
  2 件のコメント
madhan ravi
madhan ravi 2019 年 1 月 30 日
What's the issue ? sorry it's not clear
axis equal %?
M Fahd
M Fahd 2019 年 1 月 30 日
Hi, the problem was that the secondary axes (both X and Y) were not aligned with the plot area edge. However, the issue is resolved now. Thank you.

サインインしてコメントする。

採用された回答

Cris LaPierre
Cris LaPierre 2019 年 1 月 30 日
It looks like you already found this page. However, your code left out the part about setting the position of axes 2 to that of axes 1:
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
This will introduce a new issue, unfortunately. The x-label for the top axis is now out of view. The other issue is that this will work great unless the figure window is resized.
  1 件のコメント
M Fahd
M Fahd 2019 年 1 月 30 日
Noted with thanks. It worked :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by