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 vues (au cours des 30 derniers jours)
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!

Réponse acceptée

Voss
Voss le 28 Mar 2022
Modifié(e) : Voss le 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';
  2 commentaires
RYXChen
RYXChen le 29 Mar 2022
This method is very perfect!It solves this problem by setting the width of coordinate area. Thank you very much for your help! I learned a lot.
Best withes!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by