Search code examples
matlabplotmatlab-figure

Plotting hidden variable/parameter


I would like to plot some parametric plots as a function of a 0-1 variable.
I can easily set up x=linspace(0,1) and define functions a(x), b(x), and plot(a,b).

What I would like to do, however, is indicated in the plotted diagrams the value of my original x parameter. I would guess there is a function to do so, although I haven't found it yet. Optionally, I could also do a color gradient with a bar along each trace for my 0-1. Can anyone point me in the right direction?


Solution

  • Good news! You can do all sort of things quite easily, let's start with defining some data:

    x = linspace(0,1);
    a = sin(5.*x);
    b = cos(6.*x);
    

    Now, we make a simple plot:

    plot(x,a,'-o',x,b,'^')
    

    The '-o' means the first series of data (a) will be plotted as line with circle markers, the '^' means the second series of data (b) will be plotted with no line, just triangles. You can find all options here.
    Next, we call:

    text(x(50)+0.03,a(50),sprintf('x = %0.3f',x(50)),'FontSize',14)
    text(x(30)+0.03,b(30),sprintf('x = %0.3f',x(30)),'FontSize',14)
    

    the text command print text at specific coordinates on the figure. For example, the first line will print in (x(30)+0.03,a(30)) the text "x = 0.495". The text string is formatted with another function sprintf, but you can just write simple text in single quote marks (').
    Finally, we can add legend by:

    legend({'sin(5x)','cos(6x)'},'FontSize',16,'Location','SouthWest')
    

    note that the text strings are in a cell array.

    And we get the result:

    Plot demo

    That's the kind of stuff you have been looking for?