Filter löschen
Filter löschen

Second x-axis overwriting previous plot

1 Ansicht (letzte 30 Tage)
Bruno Rodriguez
Bruno Rodriguez am 24 Mai 2017
Bearbeitet: dpb am 25 Mai 2017
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)

Antworten (1)

dpb
dpb am 25 Mai 2017
Bearbeitet: dpb am 25 Mai 2017
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.

Community Treasure Hunt

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

Start Hunting!

Translated by