Single boxplot for multiple group comparison - matlab

Here is the sample code that i used to compare two groups with random mean and standard deviation. However, i want to plot both groups in a single box in the box plot as shown in the attached figure where x-axis is group 1 and y-axis is group 2. I could not find any code doing this. Can some one please help me with this?
clc
clear
x=[rand(1,10) rand(1,10) rand(1,10) rand(1,10) rand(1,10) rand(1,10)];
n=10 ; xx=([1:6])'; % example
r=repmat(xx,1,n)';
g=r(:)';
positions = [1 2 3 4 5 6 ];
h=boxplot(x,g, 'positions', positions);
set(h,'linewidth',2)
set(gca,'xtick',[mean(positions(1:2)) mean(positions(3:4)) mean(positions(5:6)) ])
set(gca,'xticklabel',{'exp1','exp2','exp3'},'Fontsize',28)
color = ['c', 'y', 'c', 'y','c', 'y'];
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),color(j),'FaceAlpha',.5);
end
now i want yellow and blue for exp1 in one box as shown below.. similarly for exp2 and exp3 so on.. so 3 boxes in one boxplot..Ideally this should work for any number of experiments.

For a single two-sided boxplot, we can use the 'Orientation' property, and overlay 2 boxplots one above the other:
x = [1 2 3 4 5 6 7 1 2 3 4 5 6 7];
group = [1,1,1,1,1,1,1,2,2,2,2,2,2,2];
% we need the precntiles of the groups so the boxes will overlap.
% on each boxplot we set the width to the other boxplot hight:
p1 = prctile(x(group==1),[25 75]);
p2 = prctile(x(group==2),[25 75]);
ax = axes;
% first group is vertical:
boxplot(x(group==2),'Positions',mean(x(group==1)),...
'Orientation','vertical','Widths',p1(2)-p1(1),'Colors','r');
lims1 = axis;
hold on
% secound group is horizontal:
boxplot(x(group==1),'Positions',mean(x(group==2)),...
'Orientation','horizontal','Widths',p2(2)-p2(1),'Colors','k');
% the values of the axis are no longer relevant, since they have two
% different meanings, depend on the group. So we hide them.
ax.XAxis.Visible = 'off';
ax.YAxis.Visible = 'off';
hold off
lims2 = axis;
% because each axis represent to different things, we make sure we see
% everything:
axis([max(lims1(1),lims2(1)),...
min(lims1(2),lims2(2)),...
min(lims1(3),lims2(3)),...
max(lims1(4),lims2(4))])
To create multiple two-sided box-plots you need to use an axes for each experiment:
x = rand(10,6);
nsp = floor(size(x,2)/2); % the number of subplots
meanx = mean(x);
% we need the precntiles of the groups so the boxes will overlap.
% on each boxplot we set the width to the other boxplot hight:
width = range(prctile(x,[25; 75]));
main_ax = axes; % create a tmporary axes
% we get the measurements of the ploting area:
pos = main_ax.Position;
% and divide it to our data:
axwidth = pos(3)/nsp; % the width of each group
% the bottom left corner of each group:
corner = linspace(pos(1),pos(3)+pos(1),nsp+1);
clf % clear the area!
% now we plot each pair of boxplot on a different subplot:
for k = 1:2:size(x,2)
ax = axes('Position',[corner((k+1)/2) pos(2) axwidth pos(4)]);
hold on
% first group is vertical:
boxplot(x(:,k),'Positions',meanx(k+1),...
'Orientation','vertical','Widths',width(k+1),'Colors','r');
% secound group is horizontal:
boxplot(x(:,k+1),'Positions',meanx(k),...
'Orientation','horizontal','Widths',width(k),'Colors','k');
% the values of the y-axis are no longer relevant, since they have two
% different meanings, depend on the group. So we hide them.
ax.YAxis.Visible = 'off';
% we use the x-axis to label the pairs of boxplots:
ax.XAxis.TickLabels = ['Exp ' num2str((k+1)/2)];
% because each axis represent to different things, we make sure we see
% everything:
minx = min(min(x(:,k:k+1)))*0.1;
maxx = max(max(x(:,k:k+1)))*1.1;
axis ([minx maxx minx maxx])
hold off
box off
% set the locations to the exact same place:
bx = findobj(ax,'tag','Box'); % get the boxes
posXdif = bx(2).XData(1)-bx(1).XData(1); % get the horizontal difference
posYdif = bx(2).YData(1)-bx(1).YData(1); % get the vertical difference
bx2Xdata = get(ax.Children(2).Children,{'XData'}); % get all X-data of box 2
bx2Ydata = get(ax.Children(2).Children,{'YData'}); % get all Y-data of box 2
% substruct horizontal difference X-data:
set(ax.Children(2).Children,{'XData'},...
cellfun(#(x) x-posXdif,bx2Xdata,'UniformOutput',false))
% substruct vertical difference Y-data:
set(ax.Children(2).Children,{'YData'},...
cellfun(#(y) y-posYdif,bx2Ydata,'UniformOutput',false))
end

Related

Bar plot x-axis Matlab

I am stuck with a bar plot in Matlab. I got it to work with Matlab help and this this forum until here, but on the x-axis, there are still only 2 names. I would like to have the "names" under the bars and the "categories" where the 2 names show up now. Thank you!
values = [4 10...
11 2 3;...
4 1...
5 2 -10];
names = {'PreSplitTotalEON' 'PostSplitTotalEON'...
'PreSplitPureEON' 'PostSplitPureEON' 'PostSplitUniper';...
'PreSplitTotalRWE' 'PostSplitTotalRWE'...
'PreSplitPureRWE' 'PostSplitPureRWE' 'PostSplitInnogy'};
categories = {'EON', 'RWE'};
b = bar(values,'FaceColor','flat');
xticklabels([names(1,:)';names(2,:)'])
% This will set labels to be used for each tick of the x-axis
xticks(1:1:length([names(1,:)';names(2,:)']))
% This will set how many ticks you want on the x-axis. Here, there
% should be 48 ticks being generated. One for each piece of data you have.
xtickangle(90)
% This will rotate the label so that the labels will not overlap
% with one another. This is in degrees.
for k = 1:size(values,2) % for fancier colors.
b(k).CData = k;
end
The default way would be to use legend to display the name of each element in the group. But the position of each bar can be accessed through the XOffset and XData properties. See this answer in matlab central.
So you can use something like:
ticksList = b(1).XData+arrayfun(#(x)x.XOffset, b)';
xticks(ticksList(:))
xticklabels([names(1,:)';names(2,:)'])
to correctly display the names below each bar. However, I don't see how you want to display the names of each bar and the categories together below the bars without overlapping. You could instead display the categories on top by creating a new axes. Adding Something like:
ax1 = gca;
ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
xticks(b(1).XData)
xticklabels(categories)
I.e. the complete code would be now:
clear all
close all
values = [4 1 11 2 3; 4 1 5 2 -10];
names = {'Pre split total EON' 'Post split total EON'...
'Pre split pure EON' 'Post split pure EON' 'Post split Uniper';...
'Pre split total RWE' 'Post split total RWE'...
'Pre split pure RWE' 'Post split pure RWE' 'PostSplitInnogy'};
categories = {'EON','RWE'};
figure;
b = bar(values,'FaceColor','flat');
ticksList = b(1).XData+arrayfun(#(x)x.XOffset, b)';
xticks(ticksList(:))
xticklabels([names(1,:)';names(2,:)'])
xtickangle(90)
ax1 = gca;
ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
set(ax2, 'YTick', []);
xticks(b(1).XData)
xticklabels(categories)
for k = 1:size(values,2) % for fancier colors.
b(k).CData = k;
end
You need to observe the x-axis from the graph, and approximate the
start and end x-axis values of the bars.
On the inspection it was found that bars start from 0.54 (the gap) at
the x-axis and ends near 2.32.
Next, divide the x-axis into 12 tick positions using the command xticks,
and then mark those position with the required labels using the command xticklabels. That's all.
The required code to do so is given below.
close all
clear all
values = [4 10 ...
11 2 3; ...
4 1 ...
5 2 -10];
% just declare the names lables as a simple 1-D cell array
% remove the columns and construct as row wise cell array
names = {'PreSplitTotalEON','PostSplitTotalEON', ...
'PreSplitPureEON', 'PostSplitPureEON', 'PostSplitUniper', ...
'PreSplitTotalRWE', 'PostSplitTotalRWE', ...
'PreSplitPureRWE', 'PostSplitPureRWE', 'PostSplitInnogy'};
% declare the categories label
categories = {'EON', 'RWE'};
% construct the bar
b = bar(values,'FaceColor','flat');
%-> mark the respective xticks <-
% on close inspepection, it was found that the bars starts
% near 0.54 and end at nearly 2.31 at the x-axis
% so divide the x axis in 12 ticks within those limits.
xticks([linspace(0.54, 2.31, 12)]);
% now put the lables at those limits, that all
xticklabels({categories{1}, names{1:5}, categories{2}, names{6:10}})
xtickangle(90)
% This will rotate the label so that the labels will not overlap
% with one another. This is in degrees.
for k = 1:5 % for fancier colors.
b(k).CData = k;
end
Output
Here is a fix for your code to get the desired output (explenations inside):
values = [4 10 11 2 3;
4 1 5 2 -10];
names = {'PreSplitTotal' 'PostSplitTotal'...
'PreSplitPure' 'PostSplitPure' 'PostSplitUniper';...
'PreSplitTotal' 'PostSplitTotal'...
'PreSplitPure' 'PostSplitPure' 'PostSplitInnogy'}.'; % <-- note the transpose!
categories = {'EON', 'RWE'};
N_names = size(values,2);
ax = axes('NextPlot','add'); % <-- same as 'hold on'
col = lines(N_names); % choose your favorite colormap
% draw the bars in pairs by their 'names':
for k = 1:N_names % for fancier colors.categories
b = bar(ax,[k k+N_names+1],values(:,k),0.15,...
'FaceColor',col(k,:));
end
xticks([1:N_names (N_names+2:N_names*2+1)]) % does not crate a tick for the space between categories
xticklabels(names(:))
xtickangle(30)
% place the category name on top of the axes:
text(ceil(N_names/2)+[0 N_names+1],ax.YLim(2).*[1 1],categories,...
'VerticalAlignment','bottom','HorizontalAlignment','center')

How to plot a line on the top of a grouped bar chart?

I have a grouped bar chart and I want to compare the values .i.e , I mean I want to visualize it using lines. I tried the following code and output is also follows
Y=rand(5,5)
str = {'A'; 'B'; 'C'; 'D'; 'E';};
bar_widh=0.2;
h = bar(Y,bar_widh);
hold on;plot(Y,'b');
set(gca, 'XTickLabel',str, 'XTick',1:numel(str))
grid on
l = cell(1,5);
l{1}='P'; l{2}='Q'; l{3}='R'; l{4}='S'; l{5}='T';
legend(h,l);
I got the following output:
I want to visualize smallest quantity /larger quantity of the bar.In some cases larger value is bad. Can you help me to plot the color of the line same as the bar
I got output as follows
You can try this:
Y=rand(5,5);
str = {'A'; 'B'; 'C'; 'D'; 'E';};
bar_widh=0.2;
figure; hold on;grid on
h = bar(Y,bar_widh);
% to highlight the minimum of each group,
% copy data into a new matrix
Y_ = Y;
% find the minimum values and make the rest zeors
Y_(Y_~=repmat(min(Y_,[],1),size(Y,1),1)) = 0;
% then plot with so sort of highlighting
h2 = bar(Y_,0.5);
pause(0.1) % pause to allow bars to be drawn
% now go through each group of bars and plot the line
for i = 1:numel(h)
x = h(i).XData + h(i).XOffset; % find the x coordinates where the bars are plotted
ax = plot(x,Y(:,i)); % plot the line
% set color of the bars the same as the line
h(i).FaceColor = ax.Color;
h2(i).FaceColor = ax.Color;
end
set(gca, 'XTickLabel',str, 'XTick',1:numel(str))
legend('P','Q','R','S','T');
h(i).XData
is the center coordinates of the ith group of bars.
For example, in your case:
h(1).XData = [ 1 2 3 4 5 ]; % group P
h(2).XData = [ 1 2 3 4 5 ]; % group Q
...
h(5).XData = [ 1 2 3 4 5 ]; % group T
h(i).XOffset
is the offset value of each bar in the group from its corresponding centre coordinate.
For example, in your case:
h(1).XOffset = -0.3077; % group P
h(2).XOffset = -0.1538; % group Q
...
h(5).XOffset = 0.3077; % group T
Without highlighting the minimum values
Minimum values highlighted

Hierarchically grouped boxplot

I would like to draw a boxplot with two sets of data to compare. I am willing to use Hierarchically grouped boxplot. I could just plot one set of my data using this function. I was wondering how I can use this function to plot two sets of data together. I drew the second set of data in red one by hand to show what I am trying to plot!
My problem is that I can't put two sets of data on one graph with hold on.
Well, this is not precisely what you asked for, and does not use the function hierarchicalBoxplot, but it may be a workaround. I demonstrate it using MATLAB example data:
load carsmall
% cleaning the data a little
Origin = categorical(cellstr(Origin));
MPG(Origin=='Italy') = [];
Origin(Origin=='Italy') = [];
% this part is just for readability:
data1 = MPG;
groups1 = Origin;
data2 = MPG*3;
groups2 = Origin;
% And we start:
% =============
% we need a wider figure, with a white background:
figure('Color',[1 1 1],'Position',[178 457 1114 521])
main_ax = axes; % create a tmporary axes
% we get the measurements of the ploting area:
pos = main_ax.Position;
% and divide it to our data:
group_number = 6;
width = pos(3)/group_number; % the width of each group
% the bottom left corner of each group:
corner = linspace(pos(1),pos(3)+pos(1),group_number+1);
clf % clear the area!
% Now we plot everything in a loop:
for k = 1:group_number
% create a different axes for each group:
ax = axes;
boxplot(ax,data1,groups1); % plot the first set
hold on
boxplot(ax,data2,groups2) % plot the second set
% set the ylim to include all data:
ax.YLim = [min([data1; data2])-5 max([data1; data2])+10];
ax.XTickLabelRotation = 90; % rotate xlables if needed
box off
if k == 1
ylabel('Miles per Gallon (MPG)') % only for the most right axes
else
ax.YTick = [];
end
xlabel(['Group ' num2str(k)])
ax.Position = [corner(k) 0.2 width 0.7];
end
% and finally we place the title:
main_ax = axes('Position',[corner(1) 0.11 width*group_number 0.815]);
title('Miles per Gallon by Vehicle Origin')
axis off
% and this will color the data:
f = gcf;
colors = [1 0 0;0 0 1]; % red and blue
for g = 2:numel(f.Children)
for k = 1:numel(f.Children(g).Children(1).Children)
f.Children(g).Children(1).Children(k).Color = colors(1,:);
f.Children(g).Children(1).Children(k).MarkerEdgeColor = colors(1,:);
f.Children(g).Children(2).Children(k).Color = colors(2,:);
f.Children(g).Children(2).Children(k).MarkerEdgeColor = colors(2,:);
end
end
All this procedure gives:
It will probably need some final tweaks, but it's somthing to start from ;)
Edit
For a side by side view, you can plot all groups together, and just move the x-ticks:
% Making some data:
% MAKE SURE YOU UNDERSTAND HOW THE DATA IS ARRANGED WITHIN THE GRAPH
years = 6; % try to change this number
groups = 5; % try to change this number
data1 = rand(100,years);
data2 = rand(100,years)+0.3;
groups1 = randi(groups,100,1)*2-1; % groups 1 3 5 7 9
groups2 = randi(groups,100,1)*2; % groups 2 4 6 8 10
legendEntries = {'A' 'B'};
colors = [1 0 0;0 0 1]; % red and blue
% And we start:
% =============
% we need a wider figure, with a white background:
figure('Color',[1 1 1],'Position',[178 457 1400 521])
main_ax = axes; % create a temporary axes
% we get the measurements of the plotting area:
pos = main_ax.Position;
% and divide it to our data:
width = pos(3)/years; % the width of each group
% the bottom left corner of each group:
corner = linspace(pos(1),pos(3)+pos(1),years+1);
clf % clear the area!
% Now we plot everything in a loop:
for k = 1:years
% create a different axes for each group:
ax = axes;
boxplot(ax,[data1(:,k); data2(:,k)],[groups1; groups2]);
ax.XTick = 1.5:2:(groups*2-0.5); % to "combine" the groups in pairs
ax.XTickLabel = {'a','b','c','v','f'};
% set the ylim to include all data:
ax.YLim = [min([data1(:); data2(:)]) max([data1(:); data2(:)])];
box off
if k == 1
ylabel('Miles per Gallon (MPG)') % only for the most right axes
else
ax.YTick = [];
end
xlabel(num2str(2000+k)) % the labels for the years
ax.Position = [corner(k) 0.11 width 0.8];
% this will color the data:
for g = 1:2:numel(ax.Children.Children)-1
ax.Children.Children(g).Color = colors(1,:);
ax.Children.Children(g).MarkerEdgeColor = colors(1,:);
ax.Children.Children(g+1).Color = colors(2,:);
ax.Children.Children(g+1).MarkerEdgeColor = colors(2,:);
end
if k == years
% you can try to change here the index to 1:2 and see if you like it:
leg = legend(ax.Children.Children(20:21),legendEntries);
leg.Position(1) = 0.92;
end
end
% and finally we place the title:
main_ax = axes('Position',[corner(1) 0.11 width*years 0.815]);
title('Miles per Gallon by Vehicle Origin')
axis off
And we get the crowded plot:

How to fix the legend of this boxplot in matlab?

The legend of this boxplot comes with the same color! How can I fix this? How can I move the x-axis lable a little bit lower ?Thanks for your help.
close all
clc;clear;
f=figure;
Temp_O=[-0.234115422389688;-0.153751688636750;3.03158128172032;-0.746185319551222;0.491616009046725;1.17490826218458;0.495331079652895;0.757394580248284;1.28467417069223;0.710444835069366;-0.979521722186138;-0.216850422633648;0.0596632891728577;-0.525362330358090;0.681608181821661;-0.995216710339821;-0.706416688978551;-0.147700048468633;-0.145946504735073;0.355209739265580;1.25860455564176;0.970569089382961;3.99404165520844;0.433235373567272;1.37023527554759;1.45032207715449;2.00968917969203;0.840884198707613;2.08558564237223;2.05435556980046;-15.5517060656394;3.18991806590028;1.28277879106186;2.15931490153483;3.19647581545030;2.97877640768595;0.0857405478541730;-1.59362648933500;-2.18109410889313;0.751077088333943;0.795072796032814;4.18896005388773;-0.591461781602054;-0.229818549439720];
position_O = 5:5:25;
position_O=position_O';
g = [ones(10,1); 2*ones(10,1); 3*ones(10,1) ;4*ones(10,1);5*ones(4,1)];
box_O = boxplot(Temp_O,g,'colors','b','positions',position_O,'width',0.8);
h=findobj(gca,'tag','Outliers');
delete(h)
set(gca,'XTickLabel',{' '})
hold on
Temp_S=[-0.234069549668875;-0.0803021151079149;0.166729084507040;-0.991371043478263;0.320651878289472;0.118699258741257;-0.190944834558825;0.540367970198674;1.02556298920863;0.112849364285713;-0.395341229166667;0.382362326388889;-1.40591456976744;0.247202120000001;-1.33262568333333;-1.27793610544218;0.0400995141843974;-1.32333150653595;-1.84221947163121;0.407607340136054;0.264276120300749;-0.337747273809525;1.03841878571429;-1.41048786507936;0.901727821428570;-1.03012908482143;2.69786876785714;-0.691010535714286;1.66913088345865;0.684260974489794;-10.3923539047619;1.04994314285714;2.13557031632653;3.87736348701299;7.38705700000000;0.0451628482142860;-3.69094742857143;-1.14071104081633;-3.15830153968254;-4.41399970408163;6.09908001655629;0.0267684861111112;-2.67854298170732;0.925146217948717;];
position_S = 6.8:5:26.8;
position_S=position_S';
box_S = boxplot(Temp_S,g,'colors','r','positions',position_S,'width',0.8);
h=findobj(gca,'tag','Outliers');
delete(h)
legend(findobj(gca,'Tag','Box'),'Group1','Group2')
set(gca,'XTickLabel',{' '}) ;
hold off
text('Position',[5,-11],'String','S')
text('Position',[10,-11],'String','M')
text('Position',[15,-11],'String','L')
text('Position',[20,-11],'String','V')
text('Position',[25,-11],'String','C')
xlabel('Types','FontSize',10);
% set(get(gca, 'XLabel'), 'Position', [0 .2 0]); %
ylim([-10.5 7.8]);
The issue is that you're only displaying the legend for the first two boxes (yours has a total of 10 boxes) and both of these are red. The first 5 boxes that are found are red and the last 5 are blue. Instead you could use the first and last box.
%// Create the box plot
box_S = boxplot(Temp_S, g, 'colors', 'r', 'positions', position_S, 'width', 0.8);
%// Get all of the box plot objects
boxes = findobj(gca, 'Tag', 'Box');
legend(boxes([end 1]), 'Group1', 'Group2')
You could do this more robustly though with the following:
boxes = findobj(gca, 'Tag', 'box');
%// Sort by xposition
[~, ind] = sort(cellfun(#mean, get(boxes, 'XData')));
%// Apply legends to one red and one blue one.
legend(boxes(ind(1:2)), 'Group1', 'Group2');
And to move the xlabel a little lower, you can simply adjust it's Position property.
yrange = diff(get(gca, 'YLim'));
XL = get(gca, 'XLabel');
original = get(XL, 'Position');
%// Add an extra 1% padding
set(XL, 'Position', original - [0 0.01*yrange 0])
Here is a small example (I only kept the necessary stuff):
x1 = randn(44,1);
x2 = randn(44,1);
pos1 = (5:5:25)';
pos2 = (6.8:5:26.8)';
g = repelem([1 2 3 4 5], [10 10 10 10 4]);
h1 = boxplot(x1, g, 'Colors','b', 'Positions',pos1, 'Width',0.8);
hold on
h2 = boxplot(x2, g, 'Colors','r', 'Positions',pos2, 'Width',0.8);
hold off
legend([h1(5,1),h2(5,1)], {'Group1','Group2'})
To quote help boxplot:
% H = BOXPLOT(...) returns the handle H to the lines in the box plot.
% H has one column per box, consisting of the handles for the various
% parts of the box. For the traditional plotstyle, the rows correspond
% to: upper whisker, lower whisker, upper adjacent value, lower adjacent
% value, box, median, and outliers. For the compact plotstyle, the rows
% correspond to: whiskers, box, median outer, median inner, and outliers.
% If median comparison intervals are indicated with markers, H will have
% two more rows for notch lo and notch hi. If medianstyle or boxstyle
% have been set explicitly, the meaning of the rows will adjust
% accordingly.

Matlab plot label individually

I would like to make individual label for each and every tick in matlab plot. I could do this by
xtick = [1, 10, 20];
xticklabels = {'January', 'February', 'December'};
set(gca, 'XTick', xtick);
set(gca, 'XTickLabel', xticklabels);
As the strings are very long, I would like to make them in a slanting way. So I would be very happy, if anyone could help me in displaying the label in a slanting way.
Thanks
I once had a somewhat similar problem and I found an example on Matlab answers by The Mathworks. Basically you create text objects with your labels and rotate them. Otherwise there is a submission on the File Exchange here that looks pretty nice. Hope that helps!
clear
clc
% Generate some test data. Assume that the X-axis represents months.
x = 1:12;
y = 10*rand(1,length(x));
% Plot the data.
h = plot(x,y,'+');
% Reduce the size of the axis so that all the labels fit in the figure.
pos = get(gca,'Position');
set(gca,'Position',[pos(1), .2, pos(3) .65])
% Add a title.
title('This is a title')
% Set the X-Tick locations so that every other month is labeled.
Xt = 1:2:11;
Xl = [1 12];
set(gca,'XTick',Xt,'XLim',Xl);
% Add the months as tick labels.
months = ['Jan';
'Feb';
'Mar';
'Apr';
'May';
'Jun';
'Jul';
'Aug';
'Sep';
'Oct';
'Nov';
'Dec'];
ax = axis; % Current axis limits
axis(axis); % Set the axis limit modes (e.g. XLimMode) to manual
Yl = ax(3:4); % Y-axis limits
% Place the text labels
t = text(Xt,Yl(1)*ones(1,length(Xt)),months(1:2:12,:));
set(t,'HorizontalAlignment','right','VerticalAlignment','top', ...
'Rotation',45);
% Remove the default labels
set(gca,'XTickLabel','')
% Get the Extent of each text object. This
% loop is unavoidable.
for i = 1:length(t)
ext(i,:) = get(t(i),'Extent');
end
% Determine the lowest point. The X-label will be
% placed so that the top is aligned with this point.
LowYPoint = min(ext(:,2));
% Place the axis label at this point
XMidPoint = Xl(1)+abs(diff(Xl))/2;
tl = text(XMidPoint,LowYPoint,'X-Axis Label', ...
'VerticalAlignment','top', ...
'HorizontalAlignment','center');