4

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.

clabacchio
  • 1,047
  • 1
  • 17
  • 28

2 Answers2

3

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', ':')
Chris
  • 44,602
  • 16
  • 137
  • 156
2

The first output of PLOTYY is a vector of axis handles.

AX = PLOTYY(..)

AX(1) will be the handle to the first axis. AX(2) will be the handle to the second axis.

To add a plot to one of the axis, simply use PLOT or LINE.

plot(AX(1), ...)

line('parent',AX(1),'xdata',...)
siliconwafer
  • 732
  • 4
  • 9
  • thank you! but in this way how can I pass both the axes values to the **line** function? – clabacchio Feb 03 '12 at 13:53
  • You can plot on each axes independently with two calls to PLOT or LINE: plot(AX(1), ...) plot(AX(2), ...) – siliconwafer Feb 03 '12 at 14:15
  • no sorry I was referring to the fact that if i try to pass **line** as you wrote, with the variable containing the x-axis and y-axis values, it returns an error; what variable type should be passed with 'xdata'? – clabacchio Feb 03 '12 at 14:20
  • You will probably want to use a double. E.g., line('parent',AX(1),'xdata',xvals,'ydata',yvals); where xvals and yvals are doubles. – siliconwafer Feb 03 '12 at 14:39