matplotlib iterate subplot axis array through single list

前端 未结 6 1287
醉话见心
醉话见心 2020-12-13 04:08

Is there a simple/clean way to iterate an array of axis returned by subplots like

nrow = ncol = 2
a = []
fig, axs = plt.subplots(nrows=nrow, ncols=ncol)
for         


        
6条回答
  •  眼角桃花
    2020-12-13 04:35

    I am not sure when it was added, but there is now a squeeze keyword argument. This makes sure the result is always a 2D numpy array. Turning that into a 1D array is easy:

    fig, ax2d = subplots(2, 2, squeeze=False)
    axli = ax2d.flatten()
    

    Works for any number of subplots, no trick for single ax, so a little easier than the accepted answer (perhaps squeeze didn't exist yet back then).

提交回复
热议问题