Loop over a 2d subplot, as if it were 1-D

I am trying to build a lot of data using subtitles and I have no problem, but I am wondering if there is a convenience method for this.

Below is a sample code.

import numpy as np    
import math 
import matplotlib.pyplot as plt

quantities=["sam_mvir","mvir","rvir","rs","vrms","vmax"
,"jx","jy","jz","spin","m200b","m200c","m500c","m2500c"
,"xoff","voff","btoc","ctoa","ax","ay","az"]

# len(quantities) = 21, just to make the second loop expression 
# shorter in this post.

ncol = 5
nrow = math.ceil(21 / ncol)

fig, axes = plt.subplots(nrows = nrow, ncols=ncol, figsize=(8,6))

for i in range(nrow):
    for j in range(((21-i*5)>5)*5 + ((21-i*5)<5)*(21%5)):
        axes[i, j].plot(tree[quantities[i*ncol + j]]) 
        axes[i, j].set_title(quantities[i*ncol + j])

This code moves through a two-dimensional array of subheadings and stops on the 21st chart, leaving 4 panels empty. My question is, is there a built-in method to accomplish this task? For example, make an array of two-dimensional submarines and β€œsmooth” the array into 1D, then collapse the array through 1D through 0 ... 20.

The expression in the second range () is very ugly. I don’t think I will use this code. I think the trivial way is to count the number of graphs and break if count> 21. But I just wonder if there is a better (or fancy) way.

+4
2

, plt.subplots, plt.subplot(nrows, ncols, number). , . 3x3 6.

import numpy as np
import matplotlib.pyplot as plt

nrows, ncols = 3, 3

x = np.linspace(0,10,100)

fig = plt.figure()    
for i in range(1,7):
    ax = fig.add_subplot(nrows, ncols, i)
    ax.plot(x, x**i)

plt.show()

Example

, , plt.subplot(nrows, ncols, i), ( , ).

import numpy as np
import matplotlib.pyplot as plt

nrows, ncols = 3, 3

x = np.linspace(0,10,100)

fig = plt.figure()    
for i in range(1,10):
    ax = fig.add_subplot(nrows, ncols, i)
    if i < 7:
        ax.plot(x, x**i)

plt.show()

Example 2

GridSpec.

+6

subplots ndarray , :

fig, axes = plt.subplots(nrows = nrow, ncols=ncol, figsize=(8,6))
for ax in axes.flatten()[:20]:
    # do stuff to ax
+6

All Articles