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

133 次查看(过去 30 天)
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 个)

dpb
dpb 2017-6-14
编辑:dpb 2017-6-16
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')
  6 个评论
dpb
dpb 2018-9-28
编辑:dpb 2018-9-30
Sorry, don't understand the question? The above figure with Axes1 and Axes2 were built with the code I supplied at the above Answer; they are the same length as they are; the adjustment that is made is to Axes1 to shorten its height by raising bottom position and compensating shorter height value to give room for Axes2 and then set the second to the same bottom position as was Axes1 by default but then shorten its height to essentially zero (0.01).
Tanvir Alam
Tanvir Alam 2021-6-5
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)

请先登录,再进行评论。


ye pyae sone oo
ye pyae sone oo 2020-1-10
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
  5 个评论
Mckenzie Dice
Mckenzie Dice 2020-12-4
编辑:dpb 2020-12-4
Perhaps @dpb you could help me also... I want to do a similar thing but I am not having issues of axis plaement, rather I cannot get my values to plot to the correct axis. I have four data sets to plot, and three of them need to plot to the first axis and the last one needs to plot to the secondary axis. But every time I do this the only thing that shows up on the plot is that last data set and it overwrites the first three in every subplot. I think I need a hold off or to hold the axis off somewhere but I do not know where.
figure(20)
FigH= figure('Position', get(0, 'Screensize'));
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');
hold(hAx(2),'on')
plot(avri(c,:),z_plot,'k') %second axis: hAx(2)
xlim([-.05,.15])
xlabel('Richardson Number','FontSize',8)
title(c)
legend('v','u','th','avri')
fig = gcf;
fig.Position(3) = fig.Position(3) + 250;
% add legend
legend('Position',[0.15 0.95 0.01 0.02])
legend('boxoff')
c=c+1;
end
dpb
dpb 2020-12-5
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by