2017-12-14 19 views
0

私はPythonでテーブルをプロットしようとしています。私はそれが働いているなど...しかし、私は私のテーブルをプロットするとき、それは書かれている方法の最後に合格/失敗の列をプロットしていません。列がアルファベット順に表示されているようです。パンダのデータフレームの列をアルファベット順で並べ替える方法と、matplotlibテーブルのセルをマージする方法はありますか?

  1. これを無効にする方法を教えてください。
  2. 最後の列を追加するだけで、1つの行を追加したいとします。基本的には大きなチェックボックスですが、それを行うと、配列が同じ長さでなければならないというエラーが表示されますが、これは意味があります...しかし、これを回避して行を持たない大きな列を持つことはできますか?
import pandas as pd 
import matplotlib.pyplot as plt 

MinP_M=5 
Min_M=6 
Per_M=7 
Per_G=8 
Per2_M=9 
PerFlat_M=10 
MaxPL_M=11 
Max_M=12 
GF_M =13 


fig1 = plt.figure() 
fig1.set_size_inches(8.7,11.75,forward=True) 
ax1=fig1.add_subplot(111) 

ax1.axis('off') 
ax1.axis('tight') 

data2={'Min':['%s'%MinP_M,'%s'%Min_M,'',''], 
     'Typ':['%s'%Per_M,'%s'%Per_G,'%s'%Per2_M,'+/- %s'%PerFlat_M], 
     'Max':['%s'%MaxPL_M,'','%s'%Max_M,'+/- %s'%GF_M], 
     'Pass/Fail':['','','',''] 
    } 
df2 = pd.DataFrame(data2) 

the_table2=ax1.table(cellText=df2.values,colWidths=[0.15]*5,rowLabels=['A','B','C', 'D'],colLabels=df2.columns,loc='center') 

plt.show() 
+0

だらけ。ただそれを更新しました。ありがとう –

答えて

0

最初の部分は、解決するのが比較的容易です。 dictを使用してpandasデータフレームを作成すると、キーワードの順序、したがって列の順序は固定されません。正しい注文を得るには、columnsというキーワードを使用してください。 2番目の部分はやや難解だった。私が見つけた解決策hereは元のテーブルを2番目のテーブルにオーバーレイし、元のテーブルの4つのセルと同じ高さを持つ2番目のテーブルに別のセルを追加することです。そのためには、最初にテーブルインスタンスからセル辞書を取得し、テーブル行の高さを合計しなければなりません。以下のコードを参照してください:

import pandas as pd 
import matplotlib.pyplot as plt 

MinP_M=5 
Min_M=6 
Per_M=7 
Per_G=8 
Per2_M=9 
PerFlat_M=10 
MaxPL_M=11 
Max_M=12 
GF_M =13 


fig1 = plt.figure() 

##this line entirely messed up the plot for me (on Mac): 
##fig1.set_size_inches(8.7,11.75,forward=True) 

ax1=fig1.add_subplot(111) 

ax1.axis('off') 
ax1.axis('tight') 

data2={'Min':['%s'%MinP_M,'%s'%Min_M,'',''], 
     'Typ':['%s'%Per_M,'%s'%Per_G,'%s'%Per2_M,'+/- %s'%PerFlat_M], 
     'Max':['%s'%MaxPL_M,'','%s'%Max_M,'+/- %s'%GF_M], 
     'Pass/Fail':['','','',''] 
    } 

##fix the column ordering with a list: 
keys = ['Min', 'Typ', 'Max', 'Pass/Fail'] 
df2 = pd.DataFrame(data2, columns=keys) 

##defining the size of the table cells 
row_label_width = 0.05 
col_width = 0.15 
col_height = 0.05 

the_table2=ax1.table(
    cellText=df2.values, 
    colWidths=[col_width]*4, 
    rowLabels=['A','B','C', 'D'], 
    colLabels=df2.columns, 
    ##loc='center', ##this has no effect if the bbox keyword is used 
    bbox = [0,0,col_width*4,col_height*5], 
) 

celld = the_table2.get_celld() 

##getting the heights of the header and the columns: 
row_height_tot = 0 
for (i,j),cell in celld.items(): 
    if j==3 and i>0: #last column, but not the header 
     row_height_tot += cell.get_height()  

the_table3=ax1.table(
    cellText=['0'], ##cannot be empty 
    colLabels=df2.columns[-1:], 
    colWidths=[col_width], 
    bbox = [col_width*3,0,col_width,col_height*5], 
) 
the_table3.add_cell(1,0,col_width,row_height_tot)   

fig1.tight_layout() 

plt.show() 

フォーマットオプションのいくつかをオフにして、コンピュータで奇妙な結果を出さなければなりませんでした。テーブルを中央に配置する場合は、コマンドのbboxオプションで再生します。最終的な結果は次のようになります。 enter image description here

希望します。

関連する問題