How can I add a 2-column legend to a Matlab plot? -
consider following code:
t=0:.01:(2*pi); y=[sin(t);sin(t-pi/12);sin(t-pi/6);sin(t-pi/4)]; figure(1) clf subplot(6,1,5) plot(t,y) xlim([0 2*pi]) legend('1','2','3','4')
it produces following figure:
is there way change legend 2-column lay-out? be
--- 1 --- 3
--- 2 --- 4
instead of
--- 1
--- 2
--- 3
--- 4
so legend boundary lined not cross graph boundary lines.
i found gridlegend
script, prefer code directly.
you can hack sort of thing making second invisible axis on top of first, this:
t=0:.01:(2*pi); y=[sin(t);sin(t-pi/12);sin(t-pi/6);sin(t-pi/4)]; figure subplot(6,1,5) plot(t,y) xlim([0 2*pi]) l1 = legend('1', '2'); pos = l1.position; set(l1, 'position', pos - [pos(3) 0 0 0]); legend boxoff ax2 = copyobj(gca, gcf); set(ax2, 'visible', 'off', 'clipping', 'off') kids = ax2.children; set(kids, 'visible', 'off', 'clipping', 'off') set(ax2, 'children', kids([3:4 1:2])) l2 = legend(ax2, '3', '4'); legend(ax2, 'boxoff') legend boxoff
note fragile (e.g., doesn't handle window being resized on version of matlab).
Comments
Post a Comment