matplotlib同一窗口中的数字序列 [英] matplotlib sequence of figures in the same window

查看:52
本文介绍了matplotlib同一窗口中的数字序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试一个算法,我想使用 matplotlib 生成一系列显示中间结果的图形.

I'm testing an algorithm and I'd like to produce a sequence of figures displaying intermediate results using matplotlib.

我不需要动画,也不需要屏幕上的多个图形,也不需要子图.

I'm not needing animations, nor multiple figures all on the screens, nor subplots.

我只想生成一系列图形(可能使用pyplot),完成后,将显示一个窗口.然后我想使用箭头在数字序列中导航.

I'd just like to produce a sequence of figures (possibly using pyplot), and when I'm done, a single window is shown. Then I'd like to navigate in the sequence of figures using the arrows.

我该怎么做?

我尝试搜索,但是我只能在屏幕上找到子图或多个图形.

I tried to search, but I can only find subplot or multiple figures on the screen.

谢谢

推荐答案

最通用的方法是在同一图中创建一系列轴,并且一次只显示一个轴.

The most general approach is to create a sequence of axes in the same figure, and only display one at a time.

这是一个例子(向左和向右箭头键控制显示哪个图):

Here's an example of that (The left and right arrow keys control which plot is displayed):

import matplotlib.pyplot as plt
import numpy as np

def main():
    x = np.linspace(0, 10, 100)
    axes = AxesSequence()
    for i, ax in zip(range(3), axes):
        ax.plot(x, np.sin(i * x))
        ax.set_title('Line {}'.format(i))
    for i, ax in zip(range(5), axes):
        ax.imshow(np.random.random((10,10)))
        ax.set_title('Image {}'.format(i))
    axes.show()

class AxesSequence(object):
    """Creates a series of axes in a figure where only one is displayed at any
    given time. Which plot is displayed is controlled by the arrow keys."""
    def __init__(self):
        self.fig = plt.figure()
        self.axes = []
        self._i = 0 # Currently displayed axes index
        self._n = 0 # Last created axes index
        self.fig.canvas.mpl_connect('key_press_event', self.on_keypress)

    def __iter__(self):
        while True:
            yield self.new()

    def new(self):
        # The label needs to be specified so that a new axes will be created
        # instead of "add_axes" just returning the original one.
        ax = self.fig.add_axes([0.15, 0.1, 0.8, 0.8], 
                               visible=False, label=self._n)
        self._n += 1
        self.axes.append(ax)
        return ax

    def on_keypress(self, event):
        if event.key == 'right':
            self.next_plot()
        elif event.key == 'left':
            self.prev_plot()
        else:
            return
        self.fig.canvas.draw()

    def next_plot(self):
        if self._i < len(self.axes):
            self.axes[self._i].set_visible(False)
            self.axes[self._i+1].set_visible(True)
            self._i += 1

    def prev_plot(self):
        if self._i > 0:
            self.axes[self._i].set_visible(False)
            self.axes[self._i-1].set_visible(True)
            self._i -= 1

    def show(self):
        self.axes[0].set_visible(True)
        plt.show()

if __name__ == '__main__':
    main()

如果它们都是相同类型的情节,则只需更新所涉及艺术家的数据即可.如果每个图中的项目数量相同,这将特别容易.现在我将省略一个示例,但是如果上面的示例过于占用内存,那么仅更新艺术家的数据将变得轻巧得多.

If they're all the same type of plot, you could just update the data of the artists involved. This is especially easy if you have the same number of items in each plot. I'll leave out an example for the moment, but if the example above is too memory-hungry, just updating the data of the artists will be considerably lighter.

这篇关于matplotlib同一窗口中的数字序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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