Python Matplotlib:从多个子地块中获取单个子地块

Python Matplotlib:从多个子地块中获取单个子地块,python,matplotlib,Python,Matplotlib,我有一个应用程序,其中我有一个带有九个线图子图(3x3)的图形,我想让用户选择一个图表,并打开一个小的wx Python应用程序,以允许编辑和缩放指定的子图 是否可以从所选子绘图中获取所有信息,即轴标签、轴格式、线条、刻度大小、刻度标签等,并在wx应用程序的画布上快速绘图 我当前的解决方案太长太大,因为我只是重新绘制用户选择的绘图。我在想类似的事情,但它并不完全正确 #ax is a dictionary containing each instance of the axis sub-plot

我有一个应用程序,其中我有一个带有九个线图子图(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()
有没有办法将getp(ax)中的属性保存到一个变量中,并将该变量的选定属性与setp(ax)一起使用来构造一个新图表?考虑到调用getp(ax)时打印数据的速度有多快,我觉得必须以某种方式访问这些数据,但我甚至无法让以下代码在具有两个y轴的轴上工作:

label = ax1.yaxis.get_label()
ax2.yaxis.set_label(label)

我觉得这是不可能的,但我想我无论如何都会问。

不幸的是,在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 is 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 is 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()

下面是一个使用类存储子批(axis)的工作示例 由用户选择(缩放轴)。从存储的轴, 你可以得到你需要的所有信息。比如这个演示, 显示如何缩放和恢复子地块(轴)。那么你可以 在类中编写可以访问选定对象的其他方法 轴,以满足您的需要

import matplotlib.pyplot as plt

class Demo(object):
    """ Demo class for interacting with matplotlib subplots """
    def __init__(self):
        """ Constructor for Demo """
        self.zoomed_axis = None
        self.orig_axis_pos = None

    def on_click(self, event):
        """ Zoom or restore the selected subplot (axis) """
        # Note: event_axis is the event in the *un-zoomed* figure
        event_axis = event.inaxes
        if event_axis is None: # Clicked outside of any subplot
            return
        if event.button == 1:  # Left mouse click in a subplot
            if not self.zoomed_axis:
                self.zoomed_axis = event_axis
                self.orig_axis_pos = event_axis.get_position()
                event_axis.set_position([0.1, 0.1, 0.85, 0.85])
                # Hide all other axes
                for axis in event.canvas.figure.axes:
                    if axis is not event_axis:
                        axis.set_visible(False)
            else:
                self.zoomed_axis.set_position(self.orig_axis_pos)
                # Restore all axes
                for axis in event.canvas.figure.axes:
                    axis.set_visible(True)
                self.zoomed_axis = None
                self.orig_axis_pos = None
        else:  # any other event in a subplot
            return
        event.canvas.draw()

    def run(self):
        """ Run the demo """
        fig, axes = plt.subplots(nrows=2, ncols=2)
        for axis, color in zip(axes.flat, ['r', 'g', 'b', 'c']):
            axis.plot(range(10), color=color)
        fig.canvas.mpl_connect('button_press_event', self.on_click)
        plt.show()

def main():
    """ Main driver """
    demo = Demo()
    demo.run()

if __name__ == "__main__":
    main()

我猜如果答案不容易找到,可能有一个困难的解决方案。谢谢你的想法;这当然是另一个尝试的途径。我喜欢这个想法,但是,在最近的matplotlib版本中恢复原始布局存在问题。缩放后的绘图没有恢复到原来的大小。@Langitar-我可以使用
v1.4.3
。(请注意,当前编写的示例仅在右键单击时返回其原始大小,而不是再次左键单击。)您遇到了什么问题?好的,如果您知道自己在做什么,似乎是可行的。第二次左键单击将用缩放的位置替换已缩放子批次的原始位置,然后将无法返回。我在做这个实验的时候遇到了这个小问题。我已经解决了我遇到的大多数问题。可以在此处找到解释: