Hi everyone,
I have a problem with adding secondary Xaxis to my plot. I use MATLAB 2017b.
Here is my CODE:
X=[300 400 500 600];
X2=[4 3.5 3.0 2.5];
Y=[0.3 0.4 0.5 0.6];
figure()
plot(X,Y);
ax=gca;
ax.XAxisLocation='top';
Now how to add X2 to the bottom ?
Please help me.

 채택된 답변

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 12월 28일

0 개 추천

Here is one possible solution:
X1=[300 400 500 600];
X2=[4 3.5 3.0 2.5];
Y=[0.3 0.4 0.5 0.6];
t = tiledlayout(1,1);
ax1 = axes(t);
plot(ax1,X1,Y,'-r')
ax1.XColor = 'r';
ax1.YColor = 'r';
ax2 = axes(t);
plot(ax2,X2,Y,'ko--')
ax2.XAxisLocation = 'top';
ax2.Color = 'none';
In case, you have two different sets of Y values, then
X1=[300 400 500 600];
X2=[4 3.5 3.0 2.5];
Y1=[0.3 0.4 0.5 0.6];
Y2=10*[0.3 0.4 0.5 0.6]
Y2 = 1×4
3 4 5 6
figure()
TT = tiledlayout(1,1);
ax1 = axes(TT);
plot(ax1,X1,Y1,'-r')
ax1.XColor = 'r';
ax1.YColor = 'r';
ax2 = axes(TT);
plot(ax2,X2,Y2,'ko--')
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';

댓글 수: 3

Sudheendran
Sudheendran 2023년 12월 29일
Thank you for the quick response. That was very helpful. Just two more question. I would like to have only one curve and two x axis top and bottom. Would it be possible to modify the first plot ?
tiledlayout function is not available in MATLAB 2017b, would you be able to make any alternative code?
Sudheendran
Sudheendran 2023년 12월 29일
이동: Dyuman Joshi 2023년 12월 29일
Hi Sulaymon,
It would be very helpful for me to plot like this. Please refer to this picture.
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 12월 29일
편집: Sulaymon Eshkabilov 2023년 12월 29일
Most welcome. Glad to be of some help :)
With :) thumbs up!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 12월 29일

1 개 추천

It should also work with this syntax
X1=[300 400 500 600];
X2=[4 3.5 3.0 2.5];
Y=[0.3 0.4 0.5 0.6];
t = figure(1);
ax1 = axes(t);
plot(ax1,X1,Y,'-r')
ax1.XColor = 'r';
ax1.YColor = 'r';
ax2 = axes(t);
plot(ax2,X2,Y,'ko--')
ax2.XAxisLocation = 'top';
ax2.Color = 'none';
:

댓글 수: 1

Sudheendran
Sudheendran 2023년 12월 29일
Hi Sulaymon,
Wonderful, Thanks a lot. But is there any way to hide or remove the red or black dashed line ? Then my problem is sloved!

댓글을 달려면 로그인하십시오.

Dyuman Joshi
Dyuman Joshi 2023년 12월 29일

0 개 추천

%Random data
x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
x2 = 1:0.2:20;
y2 = x2.^2./x2.^3;
%Define an axes and plot on it
ax1 = axes;
plot(ax1, x1, y1, '-r')
%Get the position of the axes
pos = ax1.Position;
%Define another axes on top of the previous one
ax2 = axes('Position', pos, 'XAxisLocation', 'top', 'Color', 'none');
%plot on it
hold(ax2, 'on')
plot(ax2, x2,y2,'-k')
%synchronize the y-axis
linkaxes([ax1 ax2], 'y')

댓글 수: 3

Sudheendran
Sudheendran 2023년 12월 29일
Thanks a lot, But is there anyway to hide the black curve or red curve?
Yes, one approach is to use white color for the curves -
%Random data
x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
x2 = 1:0.2:20;
y2 = x2.^2./x2.^3;
%Define an axes and plot on it
ax1 = axes;
plot(ax1, x1, y1, '-w')
%Get the position of the axes
pos = ax1.Position;
%Define another axes on top of the previous one
ax2 = axes('Position', pos, 'XAxisLocation', 'top', 'Color', 'none');
%plot on it
hold(ax2, 'on')
plot(ax2, x2,y2,'-w')
%synchronize the y-axis
linkaxes([ax1 ax2], 'y')
Sudheendran
Sudheendran 2023년 12월 29일
Thank you joshi.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

릴리스

R2017b

질문:

2023년 12월 28일

댓글:

2023년 12월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by