2017-03-26 3 views
0

私が必要とするプロットの総数を知っているときにサブプロットのディメンションを計算する方法を理解しようとしています。空のサブプロット)。プロットの総数からサブプロットの正方形の大きさを決定する

たとえば、22個のサブプロットが必要な場合は、5x5のグリッドを合計25個のサブプロットに作成し、そのうち3つを空のままにしておきます。

私は22を入力して5を出すアルゴリズムを探していると思います。誰もがPythonでこれを行うための短い方法を知っています(可能であればラムダ関数かもしれません)?

答えて

1

これは、あなたが何をしようとしてのために働く必要があります(他の選択肢またはこれを行うための既製のソリューションにも開いて、私はパンダのデータフレームの辞書のために複数のサブプロット行列をやっています)。私はラムダ関数で何も試していませんが、これを修正するのは難しいでしょう。アルゴリズムがプロットする値がなくなると停止するため、空のプロットはありません。

私はこれを書いた当初リストを使っていたので、キーと値のリストに辞書を分けました。 try節までは、値をリストに変換することなく動作します。ある程度のhack-y break_testビットを使用するのではなく、空のプロットを埋めたい場合は、サブプロットのすべてのコードをtry節に入れることができます。

奇妙なブレークバージョン:

fig = plt.figure() 

# Makes organizing the plots easier 
key_list, val_list = [k, v for k, v in dict.getitems()] 

# We take advantage of the fact that int conversions always round down 
floor = int(np.sqrt(len(val_list)) 

# If the number of plots is a perfect square, we're done. 
# Otherwise, we take the next highest perfect square to build our subplots 
if floor ** 2 == len(val_list): 
    sq_chk = floor 
else: 
    sq_chk = floor + 1 

plot_count = 0 

# The try/except makes sure we can gracefully stop building plots once 
# we've exhausted our dictionary values. 
for i in range(sq_chk): 
    for j in range(sq_chk): 
     try: 
      break_test = val_list[plot_count] 
     except: 
      break 

     ax = fig.add_subplot(sq_chk, sq_chk, plot_count + 1) 
     ax.set_title(key_list[plot_count]) 

     ... 
     # Whatever you want to do with your plots 
     ... 

     plot_count += 1 

plt.show() 

ませんブレークバージョン:

fig = plt.figure() 
key_list, val_list = [k, v for k, v in dict.getitems()] 

floor = int(np.sqrt(len(dict)) 

if floor ** 2 == len(dict): 
    sq_chk = floor 
else: 
    sq_chk = floor + 1 

plot_count = 0 

# Everything from the original snippet should be nested in the try clause 
for i in range(sq_chk): 
    for j in range(sq_chk): 
     try: 

      ax = fig.add_subplot(sq_chk, sq_chk, plot_count + 1) 
      ax.set_title(key_list[plot_count]) 

      ... 
      # Whatever you want to do with your plots 
      ... 

      plot_count += 1 

     except: 
      plot_count +=1 

plt.show() 
関連する問題