필터 지우기
필터 지우기

Second x-axis overwriting previous plot

조회 수: 1 (최근 30일)
Bruno Rodriguez
Bruno Rodriguez 2017년 5월 24일
편집: dpb 2017년 5월 25일
I'm trying to add a second x-axis at the top of a figure following the documentation, but it seems to be overwriting the previous plots, and keeps plotting on the bottom axis. Not sure what's wrong since my syntax is identical to the one in the documentation. Pertinent file and code attached.
% == PLOT PROFILES ========================================================
%
figure(1) % Temperature, Potential Temperature, and RH
plot(T,altitude)
hold on
plot(theta,altitude)
Ax1 = gca; % get current axes
Ax1.XLim = [-60,60];
Ax1_pos = Ax1.Position; % Get position of current axes
ax2 = axes('Position',Ax1_pos,'XAxisLocation','top','YAxisLocation','right','Color','none');
plot(rh,altitude,'Parent',ax2)

답변 (1개)

dpb
dpb 2017년 5월 25일
편집: dpb 2017년 5월 25일
The thing in the example you didn't copy is the use of line instead of plot to draw onto the second axis. plot does a lot of preparation work behind the scenes that you don't want when adding to axes such as here.
You can use plot, but to not cause the occlusion of the first axes when you draw on top of it, you need to call hold on on that axes first.
As a side note in using Matlab to its best advantage, you can remove the loop by vectorizing the expression for theta via the "dot" operators--
theta=(273.15+T).*(p0./p).^(R/Cp)-273.15;
NB: the "." (dot) in the multiply, divide and power operations to operate element-wise over the vectors as opposed to matrix operations.

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by