如果我在同一轴上有多个图(A、B、C),是否也可以在不平移 B 和 C 的情况下平移 A?
如果没有,还有其他方法可以实现相同的目标吗?
这是一个完整的例子:
function example_panning
%# some random data to plot
N = 3;
data = cumsum(rand(1000,N)-0.5);
labels = {'A', 'B', 'C'};
%# structure used to store graphic handles
h = struct();
%# create figure
h.fig = figure();
%# create background axis (used only to provide the 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 an individual axis (transparent)
clr = lines(N);
for 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 all axes positions
hlink = linkprop(h.ax, 'Position');
setappdata(h.fig, 'graphics_linkprop',hlink)
%# show legend (attached to background axis)
h.leg = legend(h.ax(end), h.line, labels);
%# show x/y-labels on all plot axes
for i=1:N
xlabel(h.ax(i), 'time')
ylabel(h.ax(i), 'value')
end
%# create toolbar (allows to switch current axis)
h.tb = uitoolbar(h.fig);
for 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 a figure menu (also allows to switch current axis)
h.cmenu = uimenu('Label','Current Axis');
for i=1:N
h.menu(i) = uimenu(h.cmenu, 'Label',labels{i}, ...
'ForegroundColor',clr(i,:), ...
'Checked','off', 'Callback',{@toggleButton_callback,i});
end
%# start with first axis as current and enable panning tool
toggleButton_callback([], [], 1)
pan(h.fig, 'on')
%# display informational message
msg = {'Start panning/zooming as usual,', ...
'and use color buttons to change the 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 the current one and bring it forward
set(h.fig, 'CurrentAxes',h.ax(ind))
uistack(h.ax(ind), 'top')
%# make it the only one visible (excluding background axis)
set(h.ax(1:end-1), 'Visible','off', 'Color','none')
set(h.ax(ind), 'Visible','on')
%# make sure legend is always on top
uistack(h.leg, 'top')
%# inform which axis is the current one
title(h.ax(end), labels{ind})
end
end
正如评论中提到的,这个想法是为每个线图创建多个透明轴。在任何时候,只有一个轴处于活动状态,并显示该轴的限制。
一旦你选择了你想要的轴,你就可以像往常一样使用任何交互式工具(缩放、平移、..)。
我提供了两种切换当前活动轴的方法:使用带有按钮颜色的自定义工具栏与相应的绘图相匹配,或者使用添加到图形菜单栏中的常规菜单。
除此之外,代码有相当的注释,应该很容易理解。