Why axis labels disappear when trying to assign their position?
27 views (last 30 days)
Show older comments
In Matlab 2012b I need to move axis labels of my plot arbitrarily. I want this because they are not placed according to my taste by default. I tried the following workaround but I'm wondered why the label disappears no matter what the offset value is.
The modified complete code is:
clc;
clear all;
close all
x=0:0.01:10;
y=10*cos(x)+5*sin(10*x).^2;
fs=[4.5,1.5]; % figure size
%--------------------------------------------------------------------------
fh=figure;
set(fh,'Color','w')
set(fh,'Units','inches','Position',[3 3 fs])
set(fh,'PaperUnits','normalized','PaperSize',[1 1],'PaperPosition',[0 0 1 1])
ah=axes;plot(x,y,'Color',[1 0 0],'LineWidth',1)
hold on;
grid on;
box on
set(ah,'LineWidth',1)
set(ah,'FontName','Calibri','FontSize',10,'FontWeight','bold')
set(ah,'OuterPosition',[0 0 1.0 1.0])
xlh=xlabel('\itTime (s)','Units','normalized');
pos1=get(xlh,'Position');
pos1(1,2)=pos1(1,2)-0.05;
set(xlh,'Position',pos1)
ylh=ylabel('\ity_1');
What exactly want is to get xlabel a little bit closer to the axis. Something that I feel happening is that the label with it's position assigned manually will not be included in axis outer position region anymore. Am I right?
2 Comments
Accepted Answer
Amit
on 19 Jan 2014
Edited: Amit
on 19 Jan 2014
I think you need to play with the size of your figure i.e. using 'Position' property along with 'OuterPosition'. Like, I tried this and I can see xlabel.
clc;
clear all;
close all
x=0:0.01:10;
y=10*cos(x)+5*sin(10*x).^2;
fs=[4.5,1.5]; % figure size
%--------------------------------------------------------------------------
fh=figure;
set(fh,'Color','w')
set(fh,'Units','inches','Position',[3 3 fs])
set(fh,'PaperUnits','normalized','PaperSize',[1 1],'PaperPosition',[0 0 1 1])
ah=axes;plot(x,y,'Color',[1 0 0],'LineWidth',1)
hold on;
grid on;
box on
set(ah,'LineWidth',1)
set(ah,'FontName','Calibri','FontSize',10,'FontWeight','bold')
set(ah,'OuterPosition',[0 0 1 1]);
set(ah,'Position',[0.15 0.3 0.8 0.5]); % This I what I added, You need to play with this
xlh=xlabel('\itTime (s)','Units','normalized');
pos1=get(xlh,'Position');
pos1(1,2)=pos1(1,2)-0.05;
set(xlh,'Position',pos1)
ylh=ylabel('\ity_1');
Nice mods though!!
3 Comments
Amit
on 19 Jan 2014
Edited: Amit
on 19 Jan 2014
with your script, xlabel is there all the time, however it gets of the figure and you cant see it.
By default, MATLAB figure readjusts themselves once you resize the window. However, in your case you have fixed nearly all the size parameters. Thats why, when you decrease the figure size using 'Position', you relatively make space for the xlabel to appear.
alok dhaundiyal
on 16 Nov 2020
Ok, Mr Amit
Tell me how to bring back the colored axis back to plot, which get disappered after rescaling x, y, and z.
More Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!