how can I change the distance between the axis and their title?

117 Ansichten (letzte 30 Tage)
I want to change the distance between the axis and their title in a figure. is it possible?

Akzeptierte Antwort

Mischa Kim
Mischa Kim am 21 Jan. 2014
  1 Kommentar
Mohammad
Mohammad am 21 Jan. 2014
Thank you. the problem is that I don't know the position of the title cause user will enter different figures. this is my code, which doesn't seem to work.
h=hgload(name);
set(gcf,'paperunits','centimeters');
set(gcf,'paperposition',[w(1) w(2) a(1) a(1)*a(2)]);
textobj=findobj('type','text');
set(textobj,'fontunits','points');
set(textobj,'fontsize',w(3));
set(findall(gcf,'property','fontsize'),'fontsize',w(3));
set(gca,'fontsize',w(3));
set(findall(gca,'type','text'),'fontsize',w(3));
set(findall(gcf,'type','text'),'fontsize',w(3));
new_name=strrep([pathName name],'.fig','.png');
child=get(gcf,'children');
for y=1:length(child)
chi=child(y);
set(chi,'fontsize',w(3));
end
set(gca,'Units','centimeters');
pos=get(gca,'Position');
pos1=pos-[0 .02 0 0];
set(gca,'Position',pos1);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

AJ von Alt
AJ von Alt am 21 Jan. 2014
Bearbeitet: AJ von Alt am 21 Jan. 2014
A title is a text object. You can change the position of any text object by using set to change the object's 'position' property value to a new vector. See the text properties documentation for more information.
Here is a quick demo where the title is moved to the left portion of the figure.
% move a title to the left
% create a figure and plot something
figure(1);
line;
% create a title and store its handle
th = title('blah');
% get the position of the title
titlePos = get( th , 'position');
% change the x value to 0
titlePos(1) = 0;
% update the position
set( th , 'position' , titlePos);
  2 Kommentare
Mohammad
Mohammad am 21 Jan. 2014
Thank you. the problem is that I don't know the position of the title cause user will enter different figures. this is my code, which doesn't seem to work.
h=hgload(name);
set(gcf,'paperunits','centimeters');
set(gcf,'paperposition',[w(1) w(2) a(1) a(1)*a(2)]);
textobj=findobj('type','text');
set(textobj,'fontunits','points');
set(textobj,'fontsize',w(3));
set(findall(gcf,'property','fontsize'),'fontsize',w(3));
set(gca,'fontsize',w(3));
set(findall(gca,'type','text'),'fontsize',w(3));
set(findall(gcf,'type','text'),'fontsize',w(3));
new_name=strrep([pathName name],'.fig','.png');
child=get(gcf,'children');
for y=1:length(child)
chi=child(y);
set(chi,'fontsize',w(3));
end
set(gca,'Units','centimeters');
pos=get(gca,'Position');
pos1=pos-[0 .02 0 0];
set(gca,'Position',pos1);
AJ von Alt
AJ von Alt am 21 Jan. 2014
Those commands are changing the position of the axes object, not the title.
If you want to change the title object, you need to use the title handle as the first argument to set, not the axes handle.
Setting units to centimeters and changing the position by 0.2 mm doesn't do much. That is a really really small change. Unless you have a good reason to use centimeter units, I would recommend sticking with normalized units. Normalized units are much more intuitive. (0,0) is at the bottom left of the figure and (1,1) is at the top of the figure. An object at [0.25 0.5 1] would be 1/4 of the way from the left and 1/2 way from the bottom.
Try this code to give you an idea of how to modify the title position.
set(gca,'Units','normalized')
titleHandle = get( gca ,'Title' );
pos = get( titleHandle , 'position' );
pos1 = pos - [0 0.05 0]
set( titleHandle , 'position' , pos1 );

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Object Properties finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by