Align subplot x-axes

18 vues (au cours des 30 derniers jours)
Jeff
Jeff le 24 Avr 2018
Modifié(e) : Ameer Hamza le 24 Avr 2018
Hi all, could someone please help me on how to align the scales?
% data data = rand(14,2); x = data(:,1); y = data(:,2);
% dates t1 = datetime(2016,5,13); t2 = datetime(2016,8,12); t = t1:calweeks(1):t2;
% plot h = figure; graph1 = subplot(2,1,1); plot(t,x,'b-o',t,y,'r-*'); graph1.XAxis.Limits = [t1 t2]; graph1.XAxis.TickValues = t'; graph1.XAxis.TickLabelFormat = 'MMM-dd'; xtickangle(45) % graph1.XAxis.Visible = 'off'; % axes('Color','none','XColor','none'); graph2 = subplot(2,1,2); bar(t,x-y);
I would like to have the axes aligned like shown in the graph but the panels to be equal size.
Thank-you in advance.

Réponse acceptée

Jeff
Jeff le 24 Avr 2018
Modifié(e) : Jeff le 24 Avr 2018
I found a solution, realizing later that i didn't fit the graph 2 axis like I did for graph 1, and it seems to have solved the issue. Best regards, Jeff
% data data = rand(14,2); x = data(:,1); y = data(:,2);
% dates t1 = datetime(2016,5,13); t2 = datetime(2016,8,12); t = t1:calweeks(1):t2;
% plot h = figure; graph1 = subplot(2,1,1); plot(t,x,'b-o',t,y,'r-*'); graph1.XAxis.Limits = [t1 t2]; graph1.XAxis.TickValues = t'; graph1.XAxis.TickLabelFormat = 'MMM-dd'; xtickangle(45) graph1.XAxis.Visible = 'off';
graph2 = subplot(2,1,2); bar(t,x-y); graph2.XAxis.Limits = [t1 t2]; graph2.XAxis.TickValues = t'; grap2.XAxis.TickLabelFormat = 'MMM-dd';
  1 commentaire
Ameer Hamza
Ameer Hamza le 24 Avr 2018
Modifié(e) : Ameer Hamza le 24 Avr 2018
You can do this, but it will cut the width of first and last bar in half.

Connectez-vous pour commenter.

Plus de réponses (1)

Ameer Hamza
Ameer Hamza le 24 Avr 2018
The axis handle gave a Position property which specifies its location on the figure. The property is defined as [left bottom width height] as described here. Since you are only interested in changing the left starting position and width of graph2 you just need to change 1st and 3rd element of graph2.Position. You can set these values according to your requirement. As an example
graph2.Position(1) = graph1.Position(1)-0.04; % you can experiment by changing 0.04 and 0.08
graph2.Position(3) = graph1.Position(3)+0.08; % unless you achieve desired result.
You can just add these two statements at end of your script. Here I am moving left starting position of graph2 a bit left as compared to graph1 and also changing its width so that the figure will expand symmetrically.

Community Treasure Hunt

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

Start Hunting!

Translated by