Plotting two x axis in one plot, but both at the bottom.

85 views (last 30 days)
Mikkel
Mikkel on 14 Jun 2017
Edited: dpb about 11 hours ago
Hi I am looking for a way to plot two x axis but both x-axis has to be in the bottom. Like shown here in the picture. The only thing I can find is where the two x-axis are on top and bottom of the plot, where I want them both on bottom.
  2 Comments
Mikkel
Mikkel on 16 Jun 2017

Here is the picture, hope you can help me, it's the same plot but with different x axis shown.

Sign in to comment.

Answers (3)

dpb
dpb on 14 Jun 2017
Edited: dpb on 16 Jun 2017
See answer I gave <4 Y Axes> that illustrates adding a second y-axes on each the LH and RH axes. Simply make the adjustment in height and bottom locations (which are respectively, pos(2)/pos(4) instead of pos(1)/pos(3) in the position vector.
That technique makes the second x-axis small in height so you still would have to plot the data on the first in its scale but set the limits on the second to reflect actual.
Without the picture of what you're specifically trying to duplicate, I'm having difficulty in otherwise how you would make this work...do attach the figure, it'll help.
ADDENDUM It really isn't difficult; just follow the path outlined at the previous answer...
hAX=axes; % first axes, save handle
pos=get(hAX,'position') % get the position vector
pos =
0.1300 0.1100 0.7750 0.8150
pos1=pos(2); % save the original bottom position
pos(2)=pos(2)+pos1; pos(4)=pos(4)-pos1; % raise bottom/reduce height->same overall upper position
set(hAX,'position',pos) % and resize first axes
pos(2)=pos1; pos(4)=0.01; % reset bottom to original and small height
hAX(2)=axes('position',pos,'color','none'); % and create the second
ylabel(hAX(1),'Axes 1')
xlabel(hAX(1),'Axes 1')
xlabel(hAX(2),'Axes 2')
set(haX(2),'xcolor','r','ycolor','r')
  7 Comments
Tanvir Alam
Tanvir Alam on 5 Jun 2021
Edited: Walter Roberson on 18 Jan 2026 at 7:50
Hello Dpd!!
how can i plot now after typing your code over two different x-axis?
at=axes;
pos=get(at,'position');
pos1=pos(2);
pos(2)=pos(2)*2; pos(4)=pos(4)-(pos1);
set(at,'position',pos)
pos(2)=pos1; pos(4)=.003;
at(2)=axes('position',pos,'color','none');
hold(at(2),'on')
plot(at(2),t2,st)
dpb
dpb on 18 Jan 2026 at 19:54
Don't know what brought this old thread back to the forefront, and mayhaps I didn't see the above back then...
"how can i plot now ... over two different x-axis?"
That's somewhat problematical with MATLAB handle graphics designed as it is -- when you create the second axes but don't give it any height, you can't draw on it as in the original. What has to be done is to do all the plotting on axes at(1) but do a linear transformation of the x data that belongs with the second axes to the coordinates of the first.
at=axes;
pos=get(at,'position');
pos1=pos(2);
pos(2)=pos(2)*2; pos(4)=pos(4)-(pos1);
set(at,'position',pos)
pos(2)=pos1; pos(4)=.003;
at(2)=axes('position',pos,'color','none');
at(2).FontSize=at(1).FontSize;
% make up some sorta' similar data
x=rescale(rand(1,11),0.1,20); y=rescale(1:numel(x),0.1,2);
hB=plot(at(1),x,y,'b-'); % the blue line
at(1).YAxis.Direction='reverse';
Z=10; % "ZOOM" factor
xlim(at(1),[0 40])
xlim(at(2),xlim(at(1))/Z) % zoom bottom axes relative top
ylim(at(1),[0 2])
at(1).YAxis.TickLabelFormat='%0.1f';
hold(at,'on')
hR=plot(at(1),x*Z,y,'r-'); % the red line at Z factor
The x labels can be added to each axes handle.
For the particular instantiation of the random data the zoomed line isn't continuous as in the orginal picture, but that's solely owing to the data itself...the same data would look the same.
I don't know if it possible to perhaps achieve the same effect by having the second axes full height by just not a visible y axes in which case one could use the actual xlim on the second but would have to offset the y values. That's probably more difficult of a scaling problem than the above -- it's simplified owing to the zero origin; it would require the scale+offset linear transformation otherwise. polyfit would work there.

Sign in to comment.


ye pyae sone oo
ye pyae sone oo on 10 Jan 2020
Hello. Well, the discussion is a little bit old but I think I am facing the same situation, 'Question posed by Mr. Min Oo'. I think I understand his question. Let's say, we have three vectors, x1, x2 and y.
x1 = [0.06, 0.2, 0.4]; % I want to put this in X axis 1
x2 = [9, 28, 58]; % I want to put this in X axis 2
y = [120, 80, 62]; % this is y-axis.
Now, my question is what I should do if I want to have a plot with 2 x-axes, both at the bottom but representing the same for both x1 and x2. Say, at x1 = 0.06, I want x2 = 9 from X axis 2 to be directly under the value x1. I also want each value of x1 and x2 to be exactly the same upper and lower postion of X axis 1 and X axis 2. Is it manageable?
I hope it is clear.
Thanks in advance
  6 Comments
dpb
dpb on 5 Dec 2020
c=1;
while c<=20
subplot(4,5,c)
plot(av_v(:,c),z_plot, 'r') %first axis
hold on
plot(av_u(:,c),z_plot,'b') %first axis
hold on
plot(averages(:,c),z_plot,'g') %first axis
hold on
xlim([-5,15])
ylabel('height,m')
xlabel('u and v (m/s), t (k)','FontSize',8)
hAx(1)=gca;
hAx(2)=axes('Position',hAx(1).Position,'XAxisLocation','top','YAxisLocation','right');
...
Many issues here...
  1. Why use a while instead of just simple counted for loop?
  2. Save the axes handle when you create it with subplot while you're there.
  3. One you've set hold for a given axes, it won't get any "onner" by repeating it. "ON" is "ON"
  4. This Q? posed a different problem that required manually creating the second axis; your case can be solved more simply plotyy with a couple adjustments. Unfortunately, TMW got only half the job done with yyaxis--there is no complementary xxaxis and they went steerage class by not incorporating a second XAxis object.
  5. You go through this loop 20 times but every time you use and then overwrite hAx(1) and hAx(2) instead of keeping a distinct handle to the given plot axes
Very crude, but something more like:
hAx=gobjects(20,2); % allocate axes handles array for L, R
for i=1:20
subplot(4,5,i); % create the subplot
hAx(i,:)=plotyy(av_v(:,c),z_plot, 'r',avri(c,:),z_plot,'k');
hold(hAx(i,1),'on')
plot(av_u(:,c),z_plot,'b')
plot(averages(:,c),z_plot,'g')
xlim(hAx(i,1),[-5,15])
ylabel('height,m')
xlabel('u and v (m/s), t (k)','FontSize',8)
hAx(i,2).XAxisLocation='top';
xlim(hAx(i,2),[-.05,.15])
xlabel(hAx(i,2),'Richardson Number','FontSize',8)
...
% legends and so on here....
end

Sign in to comment.


dpb
dpb about 10 hours ago
Edited: dpb about 2 hours ago
Had a revelation -- the above to create the two axes can be much easier with subplot or tiledlayout. We'll use the venerable subplot here...
N=8; % number of panes to divide figure into
Z=10; % "ZOOM" factor
hAx=subplot(N,1,1:(N-1)); % divide figure into panes, group N-1 top ones into one
hAx(2)=subplot(N,1,N,'Color','none'); % create the Nth one at bottom
hAx(2).YAxis.Visible='off'; % show its x axis only
hAx(2).FontSize=hAx(1).FontSize; % default is smaller owing to height
% make up some sorta' similar data
x=rescale(rand(1,11),0.1,20); y=rescale(1:numel(x),0.1,2);
hB=plot(hAx(1),x,y,'b-');
hAx(1).YAxis.Direction='reverse';
xlim(hAx(1),[0 40])
xlim(hAx(2),xlim(hAx(1))/Z)
ylim(hAx(1),[0 2])
hAx(1).YAxis.TickLabelFormat='%0.1f';
hold(hAx,'on')
hR=plot(hAx(1),x*Z,y,'r-');
On the comment above about the scaling of the two x axes and polyfit being useful
b=polyfit(xlim(hAx(2)),xlim(hAx(1)),1)
b = 1×2
10 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
gives the coefficients of the scaling needed -- note here the intercept is 0; if one set the origin for either (or both) to something other than 0, then it would not be.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!