How can I move the Xlabel without moving the X-Axis?

364 ビュー (過去 30 日間)
Victor Hugo Garcia
Victor Hugo Garcia 2019 年 3 月 17 日
回答済み: Peter Seibold 2023 年 3 月 15 日
I am having a hard time positioning the XLabel outside the plot and centered without moving the X-axis which is in the origin, here's my code:
x = [0:0.001:0.083];
y = (169.7056)*sin((376.991)*x);
plot(x,y,'r-*');
ax = gca;
ax.XAxis.Exponent = -3;
set(gca,'XTick',0:0.005:0.090);
set(gca,'YTick',-200:10:200);
xlabel('TIEMPO EN MILISEGUNDOS','Fontsize',20,'FontWeight','bold','Color','b')
ylabel('VOLTAJE','Fontsize',20,'FontWeight','bold','Color','b')
ax.YAxisLocation = 'origin';
ax.XAxisLocation = 'origin';
grid on;
grid minor;
Here's where I would like to position the Xlabel:
Anotación 2019-03-17 125041.jpg
I would really appreciate your help!

回答 (3 件)

dpb
dpb 2019 年 3 月 17 日
編集済み: dpb 2019 年 3 月 18 日
You've got to override the default position data for the label when move the axis location to center--
Ylm=ylim; % get x, y axis limits
Xlm=xlim; % so can position relative instead of absolute
Xlb=mean(Xlim); % set horizontally at midpoint
Ylb=0.99*Ylim(1); % and just 1% below minimum y value
hXLbl=xlabel('XLabel','Position',[Xlb Ylb],'VerticalAlignment','top','HorizontalAlignment','center');
"Salt to suit" positioning...having such in a callback function for axis resize/limits change would be needed to make it dynamically update.
  4 件のコメント
Nadav Arbel
Nadav Arbel 2021 年 10 月 3 日
編集済み: Nadav Arbel 2021 年 10 月 3 日
note that the line:
Ylb=0.99*Ylim(1); % and just 1% below minimum y value
will take the xlabel abouve the y minimum value if the axis has negative values.
It is best (IMHO) to calc 1% of the y axis min value:
OnePerc = 0.01*Ylim(1)
and subtract it from the min value:
Ylb=Ylim(1) - abs(OnePerc); % and just 1% below minimum y value
Erfan Basiri
Erfan Basiri 2022 年 10 月 20 日
I think in the third line, Xlim should be changed to Xlm, and Ylim to Ylm in the next line

サインインしてコメントする。


Przemyslaw Barnas
Przemyslaw Barnas 2020 年 4 月 26 日
Just add 'Position', [x y] in your xlabel
ex.
xlabel('TIEMPO EN MILISEGUNDOS','Position',[40 -205],'Fontsize',20,'FontWeight','bold','Color','b')
  1 件のコメント
dpb
dpb 2020 年 4 月 26 日
Yes, but the solution I gave is relative to the current y-axis lower ylim value and the middle of the xlim range so will be relative to the actual position. You've just hardcoded in a fixed number. That works for a specific case but only for the specific case/data values.

サインインしてコメントする。


Peter Seibold
Peter Seibold 2023 年 3 月 15 日
If you want a constant distance from the x axis, use this code:
% Move x label closer to x-axis, here 16 px
figure(1);
clf
plot(-5:10,-1:14)
ax=gca;
axOldUnits=ax.Units;% omit this line if ax units already in pixels
ax.Units='pixels'; % omit this line if ax units already in pixels
Ypx=ax.Position(4); % get axis height in px
Ylim=ylim; % get y axis limits in data units
Xm=mean(xlim); % horizontal midpoint x axis
Data_px=(Ylim(2)-Ylim(1))/Ypx; % Data units per pixel
DistXL = Ylim(1)-Data_px*16; % Distance X-Label 16 px below x axis
xlabel('My x-label','Position',[Xm DistXL],'VerticalAlignment','top','HorizontalAlignment','center');
ax.Units=axOldUnits; % omit this line if ax units already in pixels

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by