Reputation: 279
I am plotting two boxplots with my sample data sets in MATLAB. I wanted to put a star sign between the boxplots indicating the statistical significance. When I draw this star, it is adjusted to one corner rather than between the boxes. I am attaching the boxplot with this. Any help to solve this will be appreciated.
x1 = required_data_threhold_time_for_recruitment_gdnest;
x2 = required_data_threhold_time_for_recruitment_bdnest;
x = [x1 ;x2];
g = [ones(size(x1)); 2*ones(size(x2))];
boxplot(x,g,'Labels',{'Good nest (1 lux)','Poor nest (16 lux)'});
ylabel('Time(seconds)')
yt = get(gca, 'YTick');
axis([xlim 0 ceil(max(yt)*1.2)])
set(gca, 'Xtick', 1:3);
xt = get(gca, 'XTick');
hold on
plot(xt([2 3]), [1 1]*max(yt)*1.1, '-k', mean(xt([2 3])), max(yt)*1.15, '*k')
hold off
Upvotes: 0
Views: 933
Reputation: 60474
You are plotting a line between the x-axis coordinates xt([2 3])
, with xt
the location of the tick marks. This means you are drawing a line between tick marks 2 and 3. If you observe your plot, you’ll see only two tick marks show. The third one falls out the limits towards the right. So the location of the line (and star) is exactly as expected.
Instead, use xt([1,2])
, the location of the first two tick marks.
The reason you have three tick marks is because of the line
set(gca, 'Xtick', 1:3);
which explicitly sets tick marks at x-coordinates 1, 2, and 3. The x limits for your figure are likely close to 0.5 and 2.5, since the plot boxes are plotted at integer coordinates. You can examine the value of xlim
, which you already use in the code, to know what the limits are.
I would skip this step, not explicitly set tick locations, they should be correct from the get-go.
Also, I would use the ylim
command (or set(gca,'XLim',...)
) to change the y-axis limits.
Upvotes: 1