如何在for循环中自定义imshow图形位置
how to customize imshow figure position in for loop
在下面的示例中,我尝试将六个 numpy 数组绘制为 for 循环中的图像:
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
for i in range(6):
fig = plt.figure()
a = fig.add_subplot(1, 6, i+1)
a.set_title('plot' + '_' + str(i+1))
a.axes.get_xaxis().set_visible(False)
a.axes.get_yaxis().set_visible(False)
plt.imshow(np.random.random((2,3)))
如果您在 jupyter notebook 中执行代码,您会看到所有图形都按 6 行 (6,1) 逐行打印。我应该如何更改我的 for 循环以便在(比方说)2 行 (2,3) 中按列打印我的数字?
如有任何帮助,我们将不胜感激!
将 fig = plt.figure()
移到 for
循环之外,并使用 ax = fig.add_subplot(2,3,i+1)
。这样所有的子图都会在同一个图中,而不是它们自己的图中。
在下面的示例中,我尝试将六个 numpy 数组绘制为 for 循环中的图像:
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
for i in range(6):
fig = plt.figure()
a = fig.add_subplot(1, 6, i+1)
a.set_title('plot' + '_' + str(i+1))
a.axes.get_xaxis().set_visible(False)
a.axes.get_yaxis().set_visible(False)
plt.imshow(np.random.random((2,3)))
如果您在 jupyter notebook 中执行代码,您会看到所有图形都按 6 行 (6,1) 逐行打印。我应该如何更改我的 for 循环以便在(比方说)2 行 (2,3) 中按列打印我的数字?
如有任何帮助,我们将不胜感激!
将 fig = plt.figure()
移到 for
循环之外,并使用 ax = fig.add_subplot(2,3,i+1)
。这样所有的子图都会在同一个图中,而不是它们自己的图中。