2017-10-07 8 views
1

私は1つの図に20のランダムな画像を表示しようとしています。画像は実際に表示されますが、重ねられます。私は次のものを使用しています:複数の画像を1つの図形に正しく表示する方法は?

import numpy as np 
import matplotlib.pyplot as plt 
w=10 
h=10 
fig=plt.figure() 
for i in range(1,20): 
    img = np.random.randint(10, size=(h,w)) 
    fig.add_subplot(i,2,1) 
    plt.imshow(img) 
plt.show() 

それぞれが同じサイズのグリッドレイアウト(たとえば4x5)で自然に表示されるようにしたいと思います。問題の一部は、add_subplotの引数が何を意味するのか分かりません。ドキュメントでは、引数は行数、列数、およびプロット数であることを示しています。位置決め引数はありません。さらに、プロット番号は1または2にしかなりません。これをどのように達成できますか?

答えて

8

はあなたが試すことが私のアプローチです:

import numpy as np 
import matplotlib.pyplot as plt 

w=10 
h=10 
fig=plt.figure(figsize=(8, 8)) 
columns = 4 
rows = 5 
for i in range(1, columns*rows +1): 
    img = np.random.randint(10, size=(h,w)) 
    fig.add_subplot(rows, columns, i) 
    plt.imshow(img) 
plt.show() 

結果の画像:

output_image

1

次の操作を試みることができる:この記事は重複であると考えるべきであるMultiple figures in a single windowた理由:

import matplotlib.pyplot as plt 
import numpy as np 

def plot_figures(figures, nrows = 1, ncols=1): 
    """Plot a dictionary of figures. 

    Parameters 
    ---------- 
    figures : <title, figure> dictionary 
    ncols : number of columns of subplots wanted in the display 
    nrows : number of rows of subplots wanted in the figure 
    """ 

    fig, axeslist = plt.subplots(ncols=ncols, nrows=nrows) 
    for ind,title in zip(range(len(figures)), figures): 
     axeslist.ravel()[ind].imshow(figures[title], cmap=plt.jet()) 
     axeslist.ravel()[ind].set_title(title) 
     axeslist.ravel()[ind].set_axis_off() 
    plt.tight_layout() # optional 



# generation of a dictionary of (title, images) 
number_of_im = 20 
w=10 
h=10 
figures = {'im'+str(i): np.random.randint(10, size=(h,w)) for i in range(number_of_im)} 

# plot of the images in a figure, with 5 rows and 4 columns 
plot_figures(figures, 5, 4) 

plt.show() 

しかし、これは基本的にコピーして、ここから貼り付けてあります。

こちらがお役に立てば幸いです。ここで

関連する問題