Joey Tribbiani
Joey Tribbiani

Reputation: 19

How to set origin of matlab plot at center?

I tried to plot a piecewise function in matlab.

syms x
y = piecewise(-1<x<0, x^2+2*x, 0<=x<1, 0);
fplot(y)

enter image description here

as the plot came is correct but visually its no good.

I want to set its origin at the center of the plot. How to do it?

Upvotes: 1

Views: 791

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

  1. Set same minimum and maximum space on both sides of the origin.

    ax = gca;
    MaxX = max(abs(ax.XLim));    MaxY = max(abs(ax.YLim));
    axis([-MaxX MaxX -MaxY MaxY]);
    
  2. If you also want (psuedo)-axis lines at origin, you may further use:

    xline(0,'--');  yline(0,'--');    %requires R2018b
    

    But if you actually want to move the axis location to origin then you may use the properties XAxisLocation and YaxisLocation instead of pseudo axis lines.

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

Upvotes: 4

Related Questions