How can I plot the same figure standalone and in a subplot in Matplotlib? [duplicate]

非 Y 不嫁゛ 提交于 2019-12-24 04:37:07

问题


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()

But without calling generate_plot0() and generate_plot1() twice.

Is there a good way to do it?


回答1:


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.

As a quick example:

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()

Initial state

After clicking on a subplot



来源:https://stackoverflow.com/questions/18162972/how-can-i-plot-the-same-figure-standalone-and-in-a-subplot-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!