What should I do to solve the problem that the content of the text added in the matlab drawing window is outside of the figure?

2 views (last 30 days)
My code is as follows:
bp = boxplot(data_all);
set(bp,'LineWidth',1);
ax = gca;
ax.TickLength = [0.01 0.01];
ax.LineWidth =1;
ax.XTickLabel = {"A" 1:18};
ax.XColor ='K';
ax.FontName = 'Arial';
ax.FontSize = 10;
ax.XLabel.String = 'model';
ax.XLabel.FontName = '宋体';
ax.XLabel.FontSize = 14;
ax.XLabel.FontWeight = 'bold';
ax.YLabel.String = 'p/mm';
ax.YLabel.FontName = '宋体';
ax.YLabel.FontSize = 14;
ax.YLabel.FontWeight = 'bold';
xname = cmip.Model;
xname = string(xname);
xname = ["观测值";xname];
xname = ["A" ; string(1:N)'] + '-'+ xname;
h = text(20,mean(ax.YLim),xname,'HorizontalAlignment','left');
h.FontSize = 10;
Thanks you!
Best wishes!

Accepted Answer

Voss
Voss on 28 Mar 2022
Edited: Voss on 28 Mar 2022
You can adjust the axes and text Positions after everything else. See the bottom of the code below.
% made up data:
data_all = randn(100,19);
bp = boxplot(data_all);
set(bp,'LineWidth',1);
ax = gca;
ax.TickLength = [0.01 0.01];
ax.LineWidth =1;
ax.XTickLabel = {"A" 1:18};
ax.XColor ='K';
ax.FontName = 'Arial';
ax.FontSize = 10;
ax.XLabel.String = 'model';
ax.XLabel.FontName = '宋体';
ax.XLabel.FontSize = 14;
ax.XLabel.FontWeight = 'bold';
ax.YLabel.String = 'p/mm';
ax.YLabel.FontName = '宋体';
ax.YLabel.FontSize = 14;
ax.YLabel.FontWeight = 'bold';
% made up names:
xname = {'CESIII'; 'CMCCCC'; 'CMCBBB'; 'MPIII'; 'MRIIJKLMN'};
N = numel(xname);
xname = string(xname);
xname = ["观测值";xname];
xname = ["A" ; string(1:N)'] + '-'+ xname;
h = text(20,mean(ax.YLim),xname,'HorizontalAlignment','left');
h.FontSize = 10;
% adjust the axes width (ax.Position(3)) and text x (h.Position(1))
% by the width of the text (h.Extent(3)):
% need ax and h to be in the same Units:
h.Units = 'pixels';
ax.Units = 'pixels';
% perform the adjustment:
ax.Position(3) = ax.Position(3)-h.Extent(3);
h.Position(1) = h.Position(1)-h.Extent(3);
% restore the old Units:
ax.Units = 'normalized';
h.Units = 'data';

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!