2017-06-28 10 views
1

私は、左右にテキストを正しく整列させるのに苦労しています。同じCOL幅は、右/左中央/のために使用されている:Matplotlib Textテーブル内の整列

  1. Center aligned:正しく中心に、OK
  2. Left aligned:でも、テーブルの引数としてColWidthsプロパティを省略して遊んで左
  3. に無駄なスペース私のお気に入りのソリューションthe_table._autoColumns = range(-1、len(colLabels))は状況を改善しない

私は間違っていますか?これはmatplotlibのバグですか?

よろしく、

import pandas as pd 
import matplotlib.pyplot as plt 

size_x, size_y = 12, 4 
fig = plt.figure (figsize=(size_x, size_y)) 
ax = fig.add_subplot(111) 

col_ID = pd.Series ([ "R001", "R002", "R003", "R005", "R006", "R007" ]) 
col_Title = pd.Series ([ 50*"*", 10*"-", 70*"x", "R005", "R006", "R007" ]) 
col_x  = pd.Series ([ 3,  1,  3,  4,  2,  3 ]) 
col_y  = pd.Series ([ 4,  2,  3,  2,  4,  3 ]) 

legend_df = pd.DataFrame ({ "ID" : col_ID, 
          "Title" : col_Title, 
          "X-Pos" : col_x, 
          "Y-Pos" : col_y, 
          "Value" : col_x * col_y }) 
rowLabels = legend_df.index 
colLabels = legend_df.columns 
cellText = legend_df.values 

# Center-Aligned text looks ok 
the_table = plt.table (cellText=cellText, rowLabels=rowLabels, colLabels=colLabels, loc='upper center', cellLoc="center", colWidths=[0.05, 0.52, 0.05, 0.05, 0.05, 0.05]) 

# Bogus: Same colWidths, but left OR right aligned -> unwanted space in title column 
# the_table = ax.table (cellText=cellText, rowLabels=rowLabels, colLabels=colLabels, loc='upper center', cellLoc="left", colWidths=[0.05, 0.52, 0.05, 0.05, 0.05, 0.05]) 

the_table.auto_set_font_size(False) 
the_table.set_fontsize (8) 

# Set col width automatically 
# the_table._autoColumns = range (-1,len (colLabels)) 

ax.xaxis.set_visible (False)           # Grafik-Achsen ausschalten, wir wollen nur Plot 
ax.yaxis.set_visible (False) 
# the_table.scale(2, 2) 

plt.show() 

答えて

1

ルネは、デフォルトでは、細胞は、テキストが表のセルの枠線の次のビットを開始させるために有用です両側に10%のパディングを持っています。非常に大きなセルの場合、10%はあまりにも多く、望ましくない大きなスペースにつながります。

enter image description here

これを克服するために、1は10%が実際に望ましいある他のすべての列の場合と同様に、低い値に問題の列にパディングを設定する必要があります。パディングを設定する組み込みのオプションはありませんが、列のセルをループしてそれぞれのPAD属性を変更することで、パディングを操作できます。

def set_pad_for_column(col, pad=0.1): 
    cells = [key for key in the_table._cells if key[1] == col] 
    for cell in cells: 
     the_table._cells[cell].PAD = pad 

set_pad_for_column(col=1, pad=0.01) 

これにより、2番目の列(col=1)に対してのみ1%のパディングが生成されます。

enter image description here

+0

迅速な対応をありがとう。パディング値を列幅ではなくフォントサイズに依存させる方が良いでしょうか? –

+0

テーブルクラスがmatplotlibに書き込まれる方法は、パディングをフォントサイズに依存するように変更する方法はありません。もちろん、テーブルクラスとセルクラスのサブクラス化とそれぞれのメソッドの再実装は別です。このデザインの選択について私の意見を聞いていますか? – ImportanceOfBeingErnest

+0

matplotlibのバグを開く価値はあるのでしょうか?私の視点からは、あなたの解決策は存在しますが、ユーザーが実装の内部を調整する必要がないように、正常なデフォルト/アルゴリズムが存在するはずです。あなたの意見は何ですか? –

関連する問題