0

How can I populate the subplot within the for loop? I want that the ax starts with ax[0,0] and continues with ax[0,1],ax[0,2],ax[0,3],ax[1,0],ax[1,1] ..., until the loop finnished. I tried e.g. ax[i//3, range(2)] but did not found the right solution so far.

fig, ax = plt.subplots(3,measurements//3,facecolor='w', sharex=True)
        for i in range(0,measurements):
            try:
                x = self.len.iloc[:,i]
                y = self.dfr.iloc[:,i]
                inds = ~np.logical_or(np.isnan(x), np.isnan(y))
                x = x[inds]
                y = y[inds]

                xy = np.vstack([x,y])
                z = gaussian_kde(xy)(xy)
                b, m = polyfit(x, y, 1)
  
                ax[].scatter(x, y, c=z,  s=50, cmap='jet', edgecolor='', label=None, picker=True, zorder= 2)
                ax[].plot(x, b + m * x, '-')
                ax[].set_xlim(0,100)
            except KeyError:
                pass
        plt.show()
Etiende
  • 101
  • 7
  • 1
    `fig, axs = plt.subplots(3,(measurements+2)//3,,,)` and then `for i, ax in zip(range(measurements), axs.flat):` – JohanC Oct 04 '21 at 07:46
  • works perfectly! with ```for i, ax in zip(range(measurements), ax.flat)``` – Etiende Oct 04 '21 at 07:50
  • 1
    You need `fig, axs = plt.subplots(3,(measurements+2)//3,...)` in case `measurements` wouldn't be a multiple of 3. – JohanC Oct 04 '21 at 07:51

0 Answers0