Clear Filters
Clear Filters

How to display small values in boxplot

6 views (last 30 days)
Nuria Andreu
Nuria Andreu on 30 Jun 2021
Edited: Jonas on 30 Jun 2021
Hello! I have temperature and precipitation data values from years 1931-2020. I am looking at precipitation values each 30 years. I want to create a boxplot with precipitation values, but the numbers are very small (starting on 0 up to 0.8300 inches) and do not show up in my boxplot.
I tried to edit the Y-axis to make values smaller than 0, but it doesn't work. Attached is a picture of how the boxplot comes up and the code I used.
Thank you!
hold on
xPR = [DailyPR_1; DailyPR_2; DailyPR_3];
gP1 = repmat({'1931-1960'},10321,1);
gP2 = repmat({'1961-1990'},11243,1);
gP3 = repmat({'1991-2020'},10792,1);
gPR = [gP1; gP2; gP3];
boxplot(xPR,gPR)
title('Daily Precipitation (in)')
xlabel('Years')
ylabel('Precipitation (in)')
  1 Comment
Kishan Dhakan
Kishan Dhakan on 30 Jun 2021
Can you try creating an Axes programatically (using uiaxes() function) and setting the axes property YLim or YTicks manually?

Sign in to comment.

Accepted Answer

Jonas
Jonas on 30 Jun 2021
Edited: Jonas on 30 Jun 2021
for this case a logarithmic scale would be nice. the only problem is that you have 0 values which woul map to -inf. you could set the 0 values to a positive value that is smaller than all other values, this way the median line would stay at the same place. but i would set the ylim lower border to the snallest nonzero element before elemination of the zeros
data= [0 0.1 0.5 1; 2 7 0 3];
smallestNonZero=min(data(data>0),[],'all');
data(data==0)=0.1*smallestNonZero;
boxplot(data);
set(gca,'YScale','log');
ylim([smallestNonZero, max(data(:))]);
another approach without changing values would be generate a plot with a loharithmic and a linear part along the yaxis. i saw a fileexchange submission for that purpose lately, i will add it here if i find it again
edit:
so using some more code, but a nicer result. basically two subplot sticked together
data=[0.1 0.01 0.5 0.8 1 2 3 4 4 400 20 0 5 2 1.4 0 0.8 0.6 0.3 0 0.4 0.1 0.0246 1.2 1.02 0 0 0 0 0 0.01 0.03;
0.1 0.01 0.5 0.8 1 2 3 4 4 400 20 0 5 2 104 0 8 0.6 3 0 0.04 0.1 0.0246 1.2 1.02 0 0 0 0 0 0.01 0.3].';
% this will be the lower part of the plot, we move the position manually
% later
ax1 = subplot(211);
breakpoint=min(data(data>0),[],'all');
upperPlotData=data;
upperPlotData(upperPlotData<breakpoint)=0.1*breakpoint;
boxplot(upperPlotData);
% this will be the upper part of the plot, we move the position manually
% later
ax2 = subplot(212);
boxplot(data);
set(ax1,'units','normalized','position',[0.15 0.1 0.8 0.4]);
set(ax1,'yscale','lin','ylim',[-0.01 breakpoint],'xticklabel','');
set(ax2,'units','normalized','position',[ax1.Position(1) sum(ax1.Position(2:2:end)) ax1.Position(3:4)]);
set(ax2,'yscale','log','ylim',ylim().*[0 1]+[breakpoint 0],'xticklabel','');
ax1.XLabel.String='xlabel';
ax1.YLabel.String='ylabel';
ax1.YLabel.Units='normalized';
ax1.YLabel.Position(2)=ax1.YLabel.Position(2)+sum(ax1.Position(2:2:4));
ax2.Title.String='title';
set(ax1,'xticklabel',ax1.XTick);
set(ax2,'xticklabel','');
linkaxes([ax1 ax2],'x');
% delete one of the ticks to avoid overlap
ax2.YTick(1)=[];
leading to this:

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!