How to set the origin to the center of the axes in Matlab
Asked Answered
K

3

9

When I plot a function f(x) in Matlab, for example, the sine function, the graph I get is this:

enter image description here

I want to plot it in a rather different way, such as this, generated with Mathematica:

enter image description here

Note the axes position (together with the ticks), and the x and y labels position.

Any help would be very appreciated.

Keepsake answered 4/7, 2016 at 14:15 Comment(0)
S
10

Because not all readers have the latest version of MATLAB, I decided to make this answer a little bit more general, so now it's a function, that get as input the handle to the figure to manipulate, and sets its origin in the center:

function AxesOrigin(figureh)
% set the origin of a 2-D plot to the center of the axes

figureh.Color = [1 1 1];
% get the original properties:
del_props =  {'Clipping','AlignVertexCenters','UIContextMenu','BusyAction',...
    'BeingDeleted','Interruptible','CreateFcn','DeleteFcn','ButtonDownFcn',...
    'Type','Tag','Selected','SelectionHighlight','HitTest','PickableParts',...
    'Annotation','Children','Parent','Visible','HandleVisibility','XDataMode',...
    'XDataSource','YDataSource','ZData','ZDataSource'};
lineprop = figureh.CurrentAxes.Children.get;
lineprop = rmfield(lineprop,del_props);

x = lineprop.XData;
y = lineprop.YData;
old_XTick = figureh.CurrentAxes.XTick;
old_YTick = figureh.CurrentAxes.YTick;
old_Xlim = figureh.CurrentAxes.XLim;
old_Ylim = figureh.CurrentAxes.YLim;

% check that the origin in within the data points
assert(min(x)<0 && max(x)>0 && min(y)<0 && max(y)>0,'The data do not cross the origin')

figureh.CurrentAxes.Children.delete
axis off

% Create Q1 axes
axes('Parent',figureh,...
    'Position',[0.5 0.5 0.4 0.4],...
    'XTick',old_XTick(old_XTick>0),...
    'YTick',old_YTick(old_YTick>0));
xlim([0 max(old_XTick)]);
ylim([0 max(old_YTick)]);

% Create Q3 axes
axes1 = axes('Parent',figureh,...
    'YAxisLocation','right',...
    'XAxisLocation','top',...
    'Position',[0.1 0.1 0.4 0.4],...
    'XTick',old_XTick(old_XTick<0),...
    'YTick',old_YTick(old_YTick<0));
xlim(axes1,[min(old_XTick) 0]);
ylim(axes1,[min(old_YTick) 0]);

% Create real axes
axes2 = axes('Parent',figureh,...
    'Position',[0.1 0.1 0.8 0.8]);
hold(axes2,'on');
axis off

plot(x,y,'Parent',axes2)
set(axes2.Children,lineprop)
xlim(axes2,old_Xlim);
ylim(axes2,old_Ylim);
end

It removes the original axes and put two others to create an 'origin-like' view. It's not perfect, and more like a basic idea for a workaround, that should be tweaked for the specific purpose, but it may be a good place to start with if you running 2015a or earlier.

Demonstration:

x=-2*pi:0.1:2*pi;
h = figure();
plot(x,sin(x),':or');

This code creates this output: sin function before

and after using the function above:

AxesOrigin(h)

we get the result: sin function after

Selfeducated answered 4/7, 2016 at 15:29 Comment(0)
B
9

Starting from MATLAB 2015b (according to the release notes) you can use the 'origin' option for the XAxisLocation and YAxisLocation property. So add this to your code:

ax = gca;                        % gets the current axes
ax.XAxisLocation = 'origin';     % sets them to zero
ax.YAxisLocation = 'origin';     % sets them to zero
ax.Box = 'off';                  % switches off the surrounding box
ax.XTick = [-3 -2 -1 0 1 2 3];   % sets the tick marks
ax.YTick = [-1 -0.5 0 0.5 1];    % sets the tick marks

Source

Boracic answered 4/7, 2016 at 14:22 Comment(4)
I get the following message: While setting the 'XAxisLocation' property of Axes: 'origin' is not a valid value. Use one of these values: 'bottom' | 'top'. Keepsake
Works form me on 2015b, what version are you working with? Or is your plot 3d?Boracic
R2015a, win 64-bit. Anyway, what is the equivalent of origin when using the explicit position argument?Keepsake
Sry, it is your version, origin is new in 2015b, and i do not think you can set this with an explicit argumentBoracic
K
0

This works for me:

ha = gca;
ha.XAxisLocation = 'origin';
ha.YAxisLocation = 'origin';

Based on the help page "Display Axis Lines Through Origin" https://www.mathworks.com/help/matlab/creating_plots/display-axis-lines-through-origin.html.

Kreplach answered 30/1, 2017 at 1:0 Comment(1)
The link is dead. Besides, how is this different from marco's answer?Follmer

© 2022 - 2024 — McMap. All rights reserved.