Matplotlib:从多个子图中抓取单个子图

hot*_*ana 7 python matplotlib

我有一个应用程序,我有一个图形与九个线图子图(3x3),我想让用户选择一个图表,并打开一个小的Wx Python应用程序,允许编辑和缩放指定的子图情节.

是否可以从选定的子图中获取所有信息,即轴标签,轴格式,线条,刻度尺寸,刻度标签等,并在wx应用程序的画布上快速绘制它?

我目前的解决方案太长而且笨重,因为我只是重新做用户选择的情节.我在想这样的事情,但它做得不对.

#ax is a dictionary containing each instance of the axis sub-plot
selected_ax = ax[6]
wx_fig = plt.figure(**kwargs)
ax = wx_fig.add_subplots(111)
ax = selected_ax
plt.show()
Run Code Online (Sandbox Code Playgroud)

有没有办法将属性从getp(ax)保存到变量,并使用setp(ax)的变量的选定属性构建新图表?我觉得这些数据必须以某种方式访问​​,考虑到当你调用getp(ax)时它的打印速度有多快,但是我甚至无法使用以下代码来处理具有两个y轴的轴:

label = ax1.yaxis.get_label()
ax2.yaxis.set_label(label)
Run Code Online (Sandbox Code Playgroud)

我有一种感觉,这是不可能的,但我想我还是会问.

Joe*_*ton 4

不幸的是,在 matplotlib 中克隆一个轴或在多个轴之间共享艺术家是很困难的。(并非完全不可能,但重做剧情会更简单。)

但是,像下面这样的情况又如何呢?

当您左键单击子图时,它将占据整个图形,而当您右键单击时,您将“缩小”以显示其余子图......

import matplotlib.pyplot as plt

def main():
    fig, axes = plt.subplots(nrows=2, ncols=2)
    for ax, color in zip(axes.flat, ['r', 'g', 'b', 'c']):
        ax.plot(range(10), color=color)
    fig.canvas.mpl_connect('button_press_event', on_click)
    plt.show()

def on_click(event):
    """Enlarge or restore the selected axis."""
    ax = event.inaxes
    if ax is None:
        # Occurs when a region not in an axis is clicked...
        return
    if event.button == 1:
        # On left click, zoom the selected axes
        ax._orig_position = ax.get_position()
        ax.set_position([0.1, 0.1, 0.85, 0.85])
        for axis in event.canvas.figure.axes:
            # Hide all the other axes...
            if axis is not ax:
                axis.set_visible(False)
    elif event.button == 3:
        # On right click, restore the axes
        try:
            ax.set_position(ax._orig_position)
            for axis in event.canvas.figure.axes:
                axis.set_visible(True)
        except AttributeError:
            # If we haven't zoomed, ignore...
            pass
    else:
        # No need to re-draw the canvas if it's not a left or right click
        return
    event.canvas.draw()

main()
Run Code Online (Sandbox Code Playgroud)