如何在Matplotlib的子图中独立绘制同一图形? [英] How can I plot the same figure standalone and in a subplot in Matplotlib?

查看:22
本文介绍了如何在Matplotlib的子图中独立绘制同一图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Python 编写一个程序来生成许多图形.其中一些既独立又与其他图形相比很有趣.生成这些图很昂贵(就运行时而言),我不想多次生成它们.有什么方法可以一次生成一个图,并将它作为子图的一部分吗?

I am writing a program in Python that generates many graphs. Some of these are interesting both standalone and also in comparison to other graphs. Generating these graphs is expensive (in terms of runtime) and I don't want to generate them more than once. Is there any way to generate a plot once, and the have it be part of a subplot?

我基本上是在寻找替代方案:

I'm basically looking for an alternative to this:

#generate standalone graphs
pylab.figure()
generate_plot0()
pylab.figure()
generate_plot1()

#generate subplot
pylab.figure()
subplot(121)
generate_plot0()
subplot(122)
generate_plot1()

但无需两次调用 generate_plot0() generate_plot1().

有什么好的方法吗?

推荐答案

一般来说,matplotlib 艺术家不能在多个轴上,轴也不能在多个图形中.(在某些情况下,您可以违反其中一些规则,但一般情况下是行不通的.)

Generally speaking, matplotlib artists can't be in more than one axes, and axes can't be in more than one figure. (In some cases, you can break some of these rules, but it won't work in general.)

因此,简短的答案是否".

Therefore, the short answer is no.

但是,您可能会考虑以下内容.您可以将有问题的图作为子图,然后绑定单击/按键/任何东西以隐藏所有其他子图并使选定的轴暂时填满整个图.

However, you might consider something like the following. You can have the plot in question as a subplot, than then bind a click/keypress/whatever to hide all of the other subplots and make the selected axes temporarily fill up the entire figure.

作为一个简单的例子:

import numpy as np
import matplotlib.pyplot as plt

def main():
    subplots = ZoomingSubplots(2, 2)
    colors = ['red', 'green', 'blue', 'cyan']
    for ax, color in zip(subplots.axes.flat, colors):
        data = (np.random.random(200) - 0.5).cumsum()
        ax.plot(data, color=color)
    subplots.fig.suptitle('Click on an axes to make it fill the figure.\n'
                 'Click again to restore it to its original position')
    plt.show()

class ZoomingSubplots(object):
    def __init__(self, *args, **kwargs):
        """All parameters passed on to 'subplots`."""
        self.fig, self.axes = plt.subplots(*args, **kwargs)
        self._zoomed = False
        self.fig.canvas.mpl_connect('button_press_event', self.on_click)

    def zoom(self, selected_ax):
        for ax in self.axes.flat:
            ax.set_visible(False)
        self._original_size = selected_ax.get_position()
        selected_ax.set_position([0.125, 0.1, 0.775, 0.8])
        selected_ax.set_visible(True)
        self._zoomed = True

    def unzoom(self, selected_ax):
        selected_ax.set_position(self._original_size)
        for ax in self.axes.flat:
            ax.set_visible(True)
        self._zoomed = False

    def on_click(self, event):
        if event.inaxes is None:
            return
        if self._zoomed:
            self.unzoom(event.inaxes)
        else:
            self.zoom(event.inaxes)
        self.fig.canvas.draw()

if __name__ == '__main__':
    main()

初始状态

这篇关于如何在Matplotlib的子图中独立绘制同一图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆