MATLAB - Pan a plot independently of other plots in the same axes -
if have multiple plots (a, b, c) on same axes, possible pan without panning b , c well?
if not, there way can accomplish same goal?
here complete example:
function example_panning %# random data plot n = 3; data = cumsum(rand(1000,n)-0.5); labels = {'a', 'b', 'c'}; %# structure used store graphic handles h = struct(); %# create figure h.fig = figure(); %# create background axis (used provide white bg) pos = get(0,'defaultaxesposition'); h.ax(n+1) = axes('parent',h.fig, 'position',pos, ... 'xtick',[], 'ytick',[], 'ztick',[], ... 'hittest','off', 'handlevisibility','callback'); %# plot each line in individual axis (transparent) clr = lines(n); i=1:n h.ax(i) = axes('parent',h.fig, 'position',pos, ... 'color','none', 'visible','off'); h.line(i) = plot(h.ax(i), data(:,i), ... 'color',clr(i,:), 'displayname',labels{i}); end %# link axes positions hlink = linkprop(h.ax, 'position'); setappdata(h.fig, 'graphics_linkprop',hlink) %# show legend (attached background axis) h.leg = legend(h.ax(end), h.line, labels); %# show x/y-labels on plot axes i=1:n xlabel(h.ax(i), 'time') ylabel(h.ax(i), 'value') end %# create toolbar (allows switch current axis) h.tb = uitoolbar(h.fig); i=1:n icon = reshape(repmat(clr(i,:),[256 1]), [16 16 3]); h.toggle(i) = uitoggletool(h.tb, 'cdata',icon, ... 'tooltipstring',labels{i}, 'state','off', ... 'clickedcallback',{@togglebutton_callback,i}); end %# create figure menu (also allows switch current axis) h.cmenu = uimenu('label','current axis'); i=1:n h.menu(i) = uimenu(h.cmenu, 'label',labels{i}, ... 'foregroundcolor',clr(i,:), ... 'checked','off', 'callback',{@togglebutton_callback,i}); end %# start first axis current , enable panning tool togglebutton_callback([], [], 1) pan(h.fig, 'on') %# display informational message msg = {'start panning/zooming usual,', ... 'and use color buttons change active plot.'}; uiwait(msgbox(msg, 'help', 'help', 'modal')) %% nested callback function function togglebutton_callback(o,e,ind) %# update toggle buttons set(h.toggle, 'state','off') set(h.toggle(ind), 'state','on') %# update context menu set(h.menu, 'checked','off') set(h.menu(ind), 'checked','on') %# make requested axis current 1 , bring forward set(h.fig, 'currentaxes',h.ax(ind)) uistack(h.ax(ind), 'top') %# make 1 visible (excluding background axis) set(h.ax(1:end-1), 'visible','off', 'color','none') set(h.ax(ind), 'visible','on') %# make sure legend on top uistack(h.leg, 'top') %# inform axis current 1 title(h.ax(end), labels{ind}) end end
as mentioned in comments, idea create multiple transparent axes 1 each line plot. @ moment, 1 axis active, , limits of axis displayed.
you can use of interactive tools (zoom, pan, ..) usual once have axis want selected.
i've included 2 ways switch current active axis: using custom toolbar buttons colors matching respective plot, or using regular menu added figure's menubar.
other that, code commented , should easy follow.
Comments
Post a Comment