Search code examples
matlabgraphicslinecoordinate-systems

Draw a line with non-Cartesian coordinates in MATLAB


MATLAB's surf command allows you to pass it optional X and Y data that specify non-cartesian x-y components. (they essentially change the basis vectors). I desire to pass similar arguments to a function that will draw a line.

How do I plot a line using a non-cartesian coordinate system?

My apologies if my terminology is a little off. This still might technically be a cartesian space but it wouldn't be square in the sense that one unit in the x-direction is orthogonal to one unit in the y-direction. If you can correct my terminology, I would really appreciate it!

EDIT:

Below better demonstrates what I mean:

The commands:

datA=1:10;
datB=1:10;
X=cosd(8*datA)'*datB;
Y=datA'*log10(datB*3);
Z=ones(size(datA'))*cosd(datB);
XX=X./(1+Z);
YY=Y./(1+Z);
surf(XX,YY,eye(10)); view([0 0 1])

produces the following graph:

Non-Cartesian Surf

Here, the X and Y dimensions are not orthogonal nor equi-spaced. One unit in x could correspond to 5 cm in the x direction but the next one unit in x could correspond to 2 cm in the x direction + 1 cm in the y direction. I desire to replicate this functionality but drawing a line instead of a surf For instance, I'm looking for a function where:

straightLine=[(1:10)' (1:10)'];
my_line(XX,YY,straightLine(:,1),straightLine(:,2))

would produce a line that traced the red squares on the surf graph.


Solution

  • I'm still not certain of what your input data are about, and what you want to plot. However, from how you want to plot it, I can help.

    When you call

    surf(XX,YY,eye(10)); view([0 0 1]);
    

    and want to get only the "red parts", i.e. the maxima of the function, you are essentially selecting a subset of the XX, YY matrices using the diagonal matrix as indicator. So you could select those points manually, and use plot to plot them as a line:

    Xplot = diag(XX);
    Yplot = diag(YY);
    plot(Xplot,Yplot,'r.-');
    

    The call to diag(XX) will take the diagonal elements of the matrix XX, which is exactly where you'll get the red patches when you use surf with the z data according to eye().

    Result:

    result from plot


    Also, if you're just trying to do what your example states, then there's no need to use matrices just to take out the diagonal eventually. Here's the same result, using elementwise operations on your input vectors:

    datA = 1:10;
    datB = 1:10;
    X2 = cosd(8*datA).*datB;
    Y2 = datA.*log10(datB*3);
    Z2 = cosd(datB);
    XX2 = X2./(1+Z2);
    YY2 = Y2./(1+Z2);
    plot(Xplot,Yplot,'rs-',XX2,YY2,'bo--','linewidth',2,'markersize',10);
    legend('original','vector')
    

    Result: new result