Python 子图的pyplot绘制方法
我有10个数字0-9的图像,每个图像包含28x28个像素,在一个数组Python 子图的pyplot绘制方法,python,matplotlib,iteration,subplot,Python,Matplotlib,Iteration,Subplot,我有10个数字0-9的图像,每个图像包含28x28个像素,在一个数组X中,形状(28**2,10) 我正在用循环中的新像素更新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.sha
X
中,形状(28**2,10)
我正在用循环中的新像素更新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()
,但对我来说也不起作用
我可以使用线条和一个轴来完成这项工作,但我无法让它在子批次上工作。使用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)
现在,在定义轴之前,您将得到丑陋的巨大的空白色轴,但这应该可以让您开始了。我找到了一个解决方案,创建了一个plot类,在每个轴上使用
.cla()
,然后使用imshow()重新定义每个轴。
谢谢这很有效,我用
plt.ion()
将这个答案合并到我的答案中。我没有发现我必须打开和关闭交互模式though@alexmcf抱歉不清楚,这实际上就是我所说的打开交互模式的意思ion
大概代表交互式模式ON(请注意粗体)。如果在循环后不必关闭If,这很有趣,因为在我的测试中,如果我不关闭它,最终的plt.show()
将不会阻塞,程序将继续,当它结束时,绘图将自动关闭。谢谢,这是我需要的提示:)对我来说fig.canvas.draw()
是必要的。
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()