How to change the axes position in matlab

225 views (last 30 days)
Chuzymatics Chuzymatics
Chuzymatics Chuzymatics on 10 Aug 2014
Edited: dpb on 11 Aug 2014
Hi Everybody! I want to be able to relocate my axes/the origin (0, 0) of my plot to the middle of the graphics window. I don't know how to manipulate the set command to do this. There must be a way. Regards
% Code explores advanced graphics properties
clf
x= 0:pi/10:pi;
angle = x.*180/pi;
y = -sind(angle);
h =plot(angle, y)
set(h, 'color', 'red')
set(h, 'marker','s')
set(h, 'LineWidth', 2)
h_axis =gca; % Manipulate theaxis next
set(h_axis, 'LineWidth', 2)
  3 Comments
Chuzymatics Chuzymatics
Chuzymatics Chuzymatics on 11 Aug 2014
Yeah, I want to set the position of the axes object
Chuzymatics Chuzymatics
Chuzymatics Chuzymatics on 11 Aug 2014
Please take a look at the attached file to understand what I am talking about. Pay attention to the position of the x and y axes. Thanks

Sign in to comment.

Answers (3)

dpb
dpb on 10 Aug 2014
Not a settable choice in handle graphics -- x-axis can be "top|bottom" and y is either "left|right" -- "center" isn't a choice. Seems strange a a weakness, granted.
Try the File Exchange to see if somebody has submitted a package with the ability; otherwise one has to make it by drawing the axis manually at the location desired or fake it by using two axes each of half the size to split in the middle, say.

Geoff Hayes
Geoff Hayes on 10 Aug 2014
Chuzymatics - if you are just trying to move the origin (0,0) to the centre of the figure, then you could try the following which just resets the axes limits so that (0,0) is in the centre. From your above code, you have the handle to the current axes
% handle to the current axes
h_axis =gca;
% determine the max absolute value along the x-axis
maxX = max(abs(get(h_axis,'XLim')));
% determine the max absolute value along the y-axis
maxY = max(abs(get(h_axis,'YLim')));
% now reset the limits for the axes so that (0,0) is in the centre
axis(h_axis,[-maxX maxX -maxY maxY]);
Or, instead of axis , you can use xlim and ylim.
Try the above and see what happens!
  3 Comments
dpb
dpb on 11 Aug 2014
See above...will have to draw the axes in the center or, hopefully, there's a File Exchange submission that does so...
Geoff Hayes
Geoff Hayes on 11 Aug 2014
Edited: Geoff Hayes on 11 Aug 2014
A similar request was made at move the axes. The code that is attached to the answer is a file called drawaxis.m...though it didn't quite do what I thought it would. I modified it in two places by replacing
if (myX_Crossing< props.XLim(1)) | (myX_Crossing> props.XLim(2))
error('Specified X crossing outside axis limits')
return
end
with
if (myX_Crossing< props.YLim(1)) || (myX_Crossing> props.YLim(2))
error('Specified X crossing outside y-axis limits')
end
and replacing
if (myY_Crossing< props.YLim(1)) | (myY_Crossing> props.YLim(2))
error('Specified Y crossing outside axis limits')
return
end
with
if (myY_Crossing< props.XLim(1)) || (myY_Crossing> props.XLim(2))
error('Specified Y crossing outside x-axis limits')
end
To use, just do as follows
% plot the function
x=0:0.0001:2*pi;
y=sin(x);
plot(x,y);
% move the axes so that the y-axis is located at pi along the
% x-axis, and the x-axis is located at 0 along the y-axis
drawaxis(gca, 'y', pi)
drawaxis(gca, 'x', 0)
Note that moving the graphic after that may cause the axes to become disrupted.

Sign in to comment.


dpb
dpb on 11 Aug 2014
Edited: dpb on 11 Aug 2014
A start...still, check File Exchange; may already be done for you...
>> x=linspace(0,2*pi,100); y=sin(x);
>> plot(x,y)
>> set(gca,'visible','off')
>> line([0 2*pi],[0 0],'color','k')
>> line([pi pi],[-1.2 1.2],'color','k')
>> xlim([0 2*pi]), ylim([-1.2 1.2])
>> text([0:pi:2*pi],-0.1*ones(1,3),num2str([0:pi:2*pi].','%0.3f'), ...
'horizontal','center','vertical','top','fontsize',8)
Add text for the vertical similarly as well as the ticks w/ a series of line segments.
But, as expected, there's a routine from Matt Figg at FileExchange --
that has rave revues...

Community Treasure Hunt

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

Start Hunting!