【问题标题】:MATLAB - Pan a plot independently of other plots in the same axesMATLAB - 独立于同一轴上的其他绘图平移绘图
【发布时间】:2013-05-02 21:10:04
【问题描述】:

如果我在同一轴上有多个图(A、B、C),是否可以在不平移 B 和 C 的情况下平移 A?

如果没有,我还有其他方法可以实现相同的目标吗?

【问题讨论】:

  • 你所说的“锅”是什么意思?听起来您想独立于其他图移动一个图,但是您如何期望平移一个图而不是其他图,并且仍然使轴上的值与数据点正确匹配?
  • @SteveL:您可以将每个“线条”绘制到不同但叠加的轴上。如果这是你想做的事情,我可以举个例子
  • @Amro 你应该提供一个这样的例子......甚至可能会改变哪个轴对象受scroll 工具影响。如果可以的话,我会给这个答案+5 - 这将是一个在互联网上漂浮的方便示例。
  • @tmpearce:完成,示例已发布 :)

标签: matlab plot interactive


【解决方案1】:

这是一个完整的例子:

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

正如 cmets 中所述,我们的想法是为每个线图创建多个透明轴。在任何时候,只有一个轴处于活动状态,并显示该轴的限制。

一旦选择了要选择的轴,您就可以像往常一样使用任何交互式工具(缩放、平移、..)。

我提供了两种切换当前活动轴的方法:使用自定义工具栏,按钮颜色与相应的绘图相匹配,或者使用添加到图形菜单栏中的常规菜单。

除此之外,代码有相当的注释,应该很容易理解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 2015-09-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2018-10-25
    相关资源
    最近更新 更多