MATLAB: How to plot connected points


How do i plot(x,y) such that the points are connected to each other instead of from the origin ?

Best Answer

  • I was able to reproduce your problem:
    x = [zeros(1,10); 1:10]; % Simulate Data

    x = x(:); % Simulate Data
    y = [zeros(1, 10); rand(1, 10)]; % Simulate Data
    y = y(:); % Simulate Data
    plot(x, y) % Reproduces Problem
    plot(x(x ~= 0), y(y ~= 0)) % Fixes Problem
    The solution is to take the non-zero values in both the x and y vectors, as I do in figure(2). That should give you the plot you want.