Info

# Legend for plotyy to display the string for data series with yleft axis line and marker one one side, and yright axis line and marker to the right of the string

1 view (last 30 days)
Ryan Hickey on 28 Jan 2016
Closed: MATLAB Answer Bot on 20 Aug 2021
I have a plotyy figure where the x axis data is used for both yleft and yright data. I created a legend for the data, with both y axis having the same color, and the left y axis has marker 'o', while the right y axis has 'x'. For the legend i want to display the left y axis line and marker, then the string indicating the data series to the right of that, followed by the line and marker of the yright axis to the right of the string. Is this doable in matlab?

Star Strider on 28 Jan 2016
Edited: Star Strider on 29 Jan 2016
I’m not certain what you want to do.
I ‘borrowed’ this code from the documentation from both legend and plotyy.
See if it works for you:
x = -pi:pi/20:pi;
y1 = sin(x);
y2 = cos(x);
figure(1)
[hAx,hLine1,hLine2] = plotyy(x,y1,x,y2);
hLine1.LineStyle = '-';
hLine2.LineStyle = '-';
hLine1.Marker = 'x';
hLine2.Marker = 'o';
legend('sin(x)','','Location','northoutside','Orientation','horizontal')

Kelly Kearney on 29 Jan 2016
You can get that effect with legendflex:
[hax, h(1,:), h(2,:)] = plotyy(1:10, rand(3,10), 1:10, rand(3,10));
set(h(1,:), 'marker', 'o');
set(h(2,:), 'marker', 'x');
set(h(2,:), {'color'}, get(h(1,:), 'color'));
[hleg, hobj] = legendflex(reshape(h', [],1), {'one','two','three','.','',''}, 'ncol', 2);
set(hobj(4), 'visible', 'off');
(The little hack at the end is necessary b/c my function doesn't like it when an entire column of text has 0 width, so I create and then hide a small text string).