Надеюсь, это поможет:
from __future__ import division
import numpy as np
import pylab as plt
def divideSquare(N):
if N<1:
return 1,1
minX = max(1,int(np.floor(np.sqrt(N))))
maxX = max(minX+1, int(np.ceil(np.sqrt(5.0/3*N))))
Nx = range(minX, maxX+1)
Ny = [np.ceil(N/y) for y in Nx]
err = [np.uint8(y*x - N) for y in Nx for x in Ny]
ind = np.argmin(err)
y = Nx[int(ind/len(Ny))]
x = Ny[ind%len(Nx)]
return min(y,x), max(y,x)
Nlist = np.arange(0,101)
empty = np.zeros_like(Nlist)
ratio = np.zeros_like(Nlist, dtype = float)
for k, N in enumerate(Nlist):
y,x = divideSquare(N)
empty[k] = y*x - N
ratio[k] = 1.0 * x/y
print y,x,y/x
plt.figure(1)
plt.clf()
y,x = divideSquare(2)
plt.subplot(y,x,1)
plt.plot(Nlist, empty, 'k')
plt.xlabel('Number of plots')
plt.ylabel('Empty squares left')
plt.subplot(y,x,2)
plt.plot(Nlist, ratio, 'r')
plt.xlabel('Number of plots')
plt.ylabel('Ratio')
def divide2or3(N):
if N<1:
return 1,1
if N%2==0:
return int(np.ceil(N/2)), 2
return int(np.ceil(N/3)), 3
Nlist = np.arange(0,101)
empty = np.zeros_like(Nlist)
ratio = np.zeros_like(Nlist, dtype = float)
for k, N in enumerate(Nlist):
y,x = divide2or3(N)
empty[k] = y*x - N
ratio[k] = 1.0 * x/y
print y,x,y/x
plt.figure(2)
plt.clf()
y,x = divide2or3(2)
plt.subplot(y,x,1)
plt.plot(Nlist, empty, 'k')
plt.xlabel('Number of plots')
plt.ylabel('Empty squares left')
plt.subplot(y,x,2)
plt.plot(Nlist, ratio, 'r')
plt.xlabel('Number of plots')
plt.ylabel('Ratio')
plt.show()
divSquare (N) пытается получить соотношение, близкое к 1, но с минимальным количеством пустых позиций.
div2or3 (N) дает сетку из 2 или 3 столбцов (или строк, если вы инвертируете x и y), но я не уверен, что это то, что вам нужно для 90 графиков.