필터 지우기
필터 지우기

plot lines with different x axes on the same MATLAB plot

조회 수: 126 (최근 30일)
Fan Yang
Fan Yang 2021년 10월 26일
댓글: Fan Yang 2021년 10월 27일
I am trying to plot two lines with diffenent x axes on the same plot, but matlab kept "avoiding" the first plot and only plot the second one.
(P.S. I checked the y1 and y2, none of them are off the scale of y axis.)
t = tiledlayout(1,1);
ax1 = axes(t);
ax2 = axes(t);
ax2 =
Axes with properties: XLim: [0 1] YLim: [0 1] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Show all properties
ax1.XAxisLocation = 'bottom';
ax2.XAxisLocation = 'top';
hold(ax1,'on');hold(ax2,'on')
x1 = (1:10);
y1 = sin(x1);
plot(ax1,x1,y1,'r')
hold on
x2 = [11:30];
y2 = cos(x2);
plot(ax2,x2,y2,'b')
The link above are the reference I based on develping this code

채택된 답변

Chris
Chris 2021년 10월 26일
편집: Chris 2021년 10월 26일
Set the axes color of the second plot to 'none' so the lower axis can show through.
t = tiledlayout(1,1);
ax1 = axes(t);
ax2 = axes(t);
ax1.XAxisLocation = 'bottom';
ax2.XAxisLocation = 'top';
x1 = (1:10);
y1 = sin(x1);
plot(ax1,x1,y1,'r')
hold on
x2 = [11:30];
y2 = cos(x2);
%% Important line here
ax2.Color = 'none';
plot(ax2,x2,y2,'b')
  댓글 수: 3
Chris
Chris 2021년 10월 26일
편집: Chris 2021년 10월 26일
@Fan Yang No problem. When an axis is generated, the background is white.
figure('Color',[.7 .7 .7])
plot(1:10,1:10)
When you set the color to 'none', the background becomes transparent.
figure('Color',[.7 .7 .7])
plot(1:10,1:10)
set(gca,'Color','none')
Your first plot was there the whole time, but the second axis was covering it up. If you execute only the steps up to the first plot, you'll see it never shows at all, until you make the other axis transparent.
Does my explanation make sense?
Fan Yang
Fan Yang 2021년 10월 27일
@Chris It does! Thanks for the explanation!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by