pyplot 绘制子图的方法

pyplot draw method for subplots

我有 10 张数字 0-9 的图像,每张图像都有 28x28 像素,包含在形状为 (28**2, 10) 的数组 X 中。

我正在用循环中的新像素更新 X,我想在每次迭代时更新我的​​绘图。

目前,我的代码将创建 100 个独立的图形。

def plot_output(X):
    """grayscale images of the digits 0-9
    in 28x28 pixels in pyplot

    Input, X is of shape (28^2, 10)
    """
    n = X.shape[1] # number of digits
    pixels = (28,28) # pixel shape
    fig, ax = plt.subplots(1,n)

    # cycle through digits from 0-9
    # X input array is reshaped for each 10 digits
    # to a (28,28) vector to plot
    for i in range(n):
        wi=X[:,i] # w = weights for digit

        wi=wi.reshape(*pixels)
        ax[i].imshow(wi,cmap=plt.cm.gist_gray,
            interpolation='gaussian', aspect='equal')
        ax[i].axis('off')
        ax[i].set_title('{0:0d}'.format(i))

    plt.tick_params(axis='x', which='both', bottom='off',
        top='off', labelbottom='off')

    plt.show()

for i in range(100):
    X = init_pix() # anything that generates a (728, 10) array
    plot_output(X)

我试过使用 plt.draw()pt.canvas.draw() 但我似乎无法正确实施。我也试过 plt.clf() 这对我也不起作用。

我可以使用线和一个轴使用 this post 来很好地完成这项工作,但我无法让它在子图中工作。

通过使用plt.ion()可以使plt.show()命令,通常是阻塞,而不是阻塞。

然后您可以使用 imshow 更新坐标轴,它们将在计算时出现在您的图中。

例如:

import numpy as np
import matplotlib.pyplot as plt

n=10

X = np.random.rand(28**2,n)

fig, ax = plt.subplots(1,n)

plt.ion()
plt.show()

for i in range(n):
    wi = X[:,1].reshape(28,28)
    ax[i].imshow(wi)

    #fig.canvas.draw()  # May be necessary, wasn't for me.

plt.ioff()  # Make sure to make plt.show() blocking again, otherwise it'll run
plt.show()  #   right through this and immediately close the window (if program exits)

在定义轴之前,您现在会得到丑陋的巨大空白色轴,但这应该可以帮助您入门。

我通过创建绘图 class 并在每个轴上使用 .cla() 然后使用 imshow()

重新定义每个轴找到了解决方案
class plot_output(object):

    def __init__(self, X):
        """grayscale images of the digits 1-9
        """
        self.X = X
        self.n = X.shape[1] # number of digits
        self.pixels = (25,25) # pixel shape
        self.fig, self.ax = plt.subplots(1,self.n)
        plt.ion()

        # cycle through digits from 0-9
        # X input vector is reshaped for each 10 digits
        # to a (28,28) vector to plot
        self.img_obj_ar = []

        for i in range(self.n):
            wi=X[:,i] # w = weights for digit

            wi=wi.reshape(*self.pixels)
            self.ax[i].imshow(wi,cmap=plt.cm.gist_gray,
                interpolation='gaussian', aspect='equal')
            self.ax[i].axis('off')
            self.ax[i].set_title('{0:0d}'.format(i))

        plt.tick_params(\
            axis='x',          # changes apply to the x-axis
            which='both',      # both major and minor ticks are affected
            bottom='off',      # ticks along the bottom edge are off
            top='off',         # ticks along the top edge are off
            labelbottom='off')

        plt.tick_params(\
            axis='y',          # changes apply to the y-axis
            which='both',      # both major and minor ticks are affected
            left='off', 
            right='off',    # ticks along the top edge are off
            labelleft='off')

        plt.show()

    def update(self, X):

        # cycle through digits from 0-9
        # X input vector is reshaped for each 10 digits
        # to a (28,28) vector to plot
        for i in range(self.n):
            self.ax[i].cla()
            wi=X[:,i] # w = weights for digit

            wi=wi.reshape(*self.pixels)
            self.ax[i].imshow(wi,cmap=plt.cm.gist_gray,
                            interpolation='gaussian', aspect='equal')
            self.ax[i].axis('off')
            self.ax[i].set_title('{0:0d}'.format(i))

        plt.draw()