Fred
Fred

Reputation: 95

Matplotlib: subplot

I have several time series signals (8x8) that I would like to plot using subplot. My data are stored in a matrix called H(x, y, N) where N is the number of points in each signal. I would like to display the 64 signals using subplots.

fig  = figure(figsize=(12,8))
time = np.arange(0, Nt, 1)

for x in range(8):
    for y in range(8):
        subplot(8,y+1,x+1)
        plot(time,H[x,y,:])

What I get is 8 signals in the first row, 4 in the second one, then 2, 2, 1, 1, 1 and 1.

Upvotes: 1

Views: 2549

Answers (1)

tmdavison
tmdavison

Reputation: 69116

That's not how subplot indexing works. From the docs to subplot:

subplot(nrows, ncols, plot_number)

Where nrows and ncols are used to notionally split the figure into nrows * ncols sub-axes, and plot_number is used to identify the particular subplot that this function is to create within the notional grid. plot_number starts at 1, increments across rows first and has a maximum of nrows * ncols.

So, you want to have nrows=8, ncols=8 and then a plot_number in the range 1-64, so something like:

nrows,ncols = 8,8
for y in range(8):
    for x in range(8):
        plot_number = 8*y + x + 1
        subplot(nrows,ncols,plot_number)
        plot(time,H[x,y,:])
        
        # Remove tick labels if not on the bottom/left of the grid
        if y<7: gca().set_xticklabels([])
        if x>0: gca().set_yticklabels([])

To remove tick labels, use gca() to get the current axes, and the set the xticklabels and yticklabels to an empty list: []

Upvotes: 2

Related Questions