Search code examples
matlabplotaxes

Choosing axis in plotyy


This should be quite simple, although I was not able to find a solution in the Matlab docs.

I have to plot two or more sets of data, that can be fit in two different ranges. So I can use plotyy to manage this.

What I want to do, is being able, once created a plot, to overwrite or simple add traces to one of the two axes, selectively. I tried to catch the parameters returned by plotyy, but I wasn't able to decypher them.

Any help is appreciated.


Solution

  • The MATLAB documentation on plotyy states that

    [ha, h1, h2] = plotyy(...)
    

    returns the handles of the two axes created in ha and the handles of the graphics objects from each plot in h1 and h2. ha(1) is the left axes and ha(2) is the right axes.

    So the first argument returned by plotyy is a handle to each of the axes created. To plot on the left axis use plot(ha(1), x, y) and to plot on the right axis use plot(ha(2), x, y).

    If you don't need the handles to the graphics objects plotted, you can just use ha = plotyy(...). Otherwise, h1 and h2 returns the handles to the lines (or other graphics object) plotted in the call to plotyy. So, following the example in the documentation, setting the line styles of the two lines can be done like so:

    set(h1, 'LineStyle', '--')
    set(h2, 'LineStyle', ':')