Codutie
Codutie

Reputation: 1075

how to customize imshow figure position in for loop

In the example below I'm trying to plot six numpy arrays as images in a for loop:

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)))

If you execute the code in a jupyter notebook you'll see that all figures are printed row wise in 6 rows (6,1). How should I change my for loop in order to print my figures column wise in (let's say) 2 rows (2,3)?

Any help would be very appreciated!

Upvotes: 0

Views: 529

Answers (1)

cullywest
cullywest

Reputation: 121

Move the fig = plt.figure() outside the for loop, and use ax = fig.add_subplot(2,3,i+1). That way all the subplots will be in the same figure, not their own figure.

Upvotes: 1

Related Questions