plot - How to set the origin to the center of the axes in Matlab -


when plot function f(x) in matlab, example, sine function, graph this:

enter image description here

i want plot in rather different way, such this, generated mathematica:

enter image description here

note axes position (together ticks), , x , y labels position.

any appreciated.

because not readers have latest version of matlab, decided make answer little bit more general, it's function, input handle figure manipulate, , sets origin in center:

function axesorigin(figureh) % set origin of 2-d plot center of axes  figureh.color = [1 1 1]; % 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 origin in within data points assert(min(x)<0 && max(x)>0 && min(y)<0 && max(y)>0,'the data not cross 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 original axes , put 2 others create 'origin-like' view. it's not perfect, , more basic idea workaround, should tweaked specific purpose, may place start if running 2015a or earlier.

demonstration:

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

this code creates output: sin function before

and after using function above:

axesorigin(h) 

we result: sin function after


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -