In the following MATLAB plot, I want to draw x-axis and y-axis, both through origin. What would be the easiest code?

x = linspace(-2*pi,2*pi,100);
y1 = sin(x);
y2 = cos(x);

I am not familiar with Matlab, but using the Python Matplotlib, I can give you the following hint:

a) set xlim to [-6.4, 6.4] and ylim to [-1.1, 1.1]

b) draw horizontal through origin: draw an arrow from [-6.4, 0] to [6.4, 0]

c) vertical axis through origin: draw an arrow from [0, -1.1] to [0, 1.1]

The code can be written easily, according to Matlab syntax.


Matlab contains properties (since R2015b) to control this.

Append your code with the following:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.