2012-12-17 2 views
3

enter image description here하기 matplotlib 테이블 서식 - 행 레이블이 그림의 외부에 있기 때문에 행 레이블 세포의 변화 폭은

나는,이 테이블에 명백한 문제가 있고 나는 그것을 해결하는 방법을 모르겠어요. 나는 아이 아티스트들에게 가서 높이와 너비 같은 것을 바꿀 수 있다는 것을 알고 있지만, 이미 시도해 보았지만 효과가 없었기 때문에 지금 나를 도울 수 있습니다. 여기

, 나는 이것을 위해 사용하고 코드입니다 그것을 읽고 너무 어렵지 않다 희망을 .... : 나는이에 대한 완벽한 해답을 내지 못했다

ind1=np.arange(5) 


figure() 
axes([0.2, 0.45, 0.7, 0.45]) 

## define different bars 
l1=bar((ind1-0.45),mean_morphing_cc[0:5],width=0.2,bottom=0,color='darkblue',yerr=[min_dif_morphing_cc[0:5],max_dif_morphing_cc[0:5]],error_kw=dict(elinewidth=2, ecolor='darkkhaki')) 

l2=bar((ind1-0.25),mean_persistence_cc[0:5],width=0.2,bottom=0,color='darkred',yerr=[min_dif_persistence_cc[0:5],max_dif_persistence_cc[0:5]],error_kw=dict(elinewidth=2, ecolor='darkkhaki')) 

l3=bar((ind1+0.05),mean_m_vs_p_cc[0:5],width=0.2,bottom=0,color='purple',yerr=[min_dif_m_vs_p_cc[0:5],max_dif_m_vs_p_cc[0:5]],error_kw=dict(elinewidth=2, ecolor='darkkhaki')) 

## print grid and a horizontal line at "0" 
grid(True, linestyle='-', which='major', color='lightgrey',alpha=0.5) 

hlines(0, -0.5,(max(ind1)+0.5), colors='k', linestyles='solid') 


ylabel('mean((cloud cover_forecast/cloud cover_observation)-1),\n mean("morphing" - "persistence")',horizontalalignment='right',multialignment='center',size='xx-small') 

xlim(-0.5,(max(ind1)+0.5)) 

xticks(ind1,[]) 

## print a legend 
legend((l1[0],l2[0],l3[0]),('mean morphing cloud cover','mean persistence cloud cover','mean morphing vs persistence error'),'lower center',ncol=2,bbox_to_anchor=(0.5,-0.92),borderpad=0.2,labelspacing=0.2,handlelength=1,handletextpad=0.2) 

leg = plt.gca().get_legend() 

ltext = leg.get_texts() # all the text.Text instance in the legend 

llines = leg.get_lines() # all the lines.Line2D instance in the legend 

frame = leg.get_frame() # the patch.Rectangle instance surrounding the legend 

frame.set_facecolor('0.90')  # set the frame face color to light gray 

plt.setp(ltext, fontsize='x-small') # the legend text fontsize 

## print the title 
title('cloud cover over- or underestimation\n morphing forecast compared to persistence',size='small') 

## print the table 
the_table=plt.table(cellText=[[str(i)[:4] for i in mean_morphing_cc[0:5]],max_morphing_cc[0:5],min_morphing_cc[0:5],mean_persistence_cc[0:5],max_persistence_cc[0:5],min_persistence_cc[0:5],mean_m_vs_p_cc[0:5],max_m_vs_p_cc[0:5],min_m_vs_p_cc[0:5]], 
        rowLabels=['morphing: mean','morphing: max','morphing: min','persistence: mean','persistence: max','persistence: min','morph vs per: mean','morph vs per: max','morph vs per: min'], 
        rowColours=['darkblue','darkblue','darkblue','darkred','darkred','darkred','purple','purple','purple'],colLabels=['t+1','t+2','t+3','t+4','t+5'],loc='bottom') 

## change cell properties 
table_props=the_table.properties() 
table_cells=table_props['child_artists'] 
for cell in table_cells: 
    cell.set_width(0.2) 
    cell.set_height(0.065) 
    cell.set_fontsize(12) 

show() 

답변

0

,하지만 난 내 목적에 유용한 솔루션을 찾았습니다.

colWidth와 테이블 폭을 모두 조정하면 rowLabel-column 폭을 축소 할 수 있습니다. source code에는 실제로는 변수 rowLabelWidth가 있지만 사용자가 설정할 수는 없습니다. 어쨌든 먼저 변경 사항이 적용된 곳을 볼 수 있도록 기존 코드를 다시 작성하려고합니다. 이 변수 형식의 원본입니다 :

## setting properties to variables to make table function easier to read 
data = [[str(i)[:4] for i in mean_morphing_cc[0:5]],max_morphing_cc[0:5],min_morphing_cc[0:5],mean_persistence_cc[0:5],max_persistence_cc[0:5],min_persistence_cc[0:5],mean_m_vs_p_cc[0:5],max_m_vs_p_cc[0:5],min_m_vs_p_cc[0:5]] 
rowLabels = ['morphing: mean','morphing: max','morphing: min','persistence: mean','persistence: max','persistence: min','morph vs per: mean','morph vs per: max','morph vs per: min'] 
rowColours = ['darkblue','darkblue','darkblue','darkred','darkred','darkred','purple','purple','purple'] 
colLabels = ['t+1','t+2','t+3','t+4','t+5'] 
loc = 'bottom' 

## without changing anything, this is what your table function would look like 
the_table=plt.table(cellText = data, 
       rowLabels = rowLabels, rowColours = rowColours, 
       colLabels = colLabels, loc = loc) 

가 여기에 우리가 너비와 colWidth를 설정하는 알아낼 수 있도록하는 데 사용할 rowLabelWidth의 소스 코드에 무엇이입니다. 는 축 ([0.2, 0.45, 0.7, 0.45), 0.7로하고 있으므로, 변수에 해당 설정한다 tb_width

tb_width = 0.7 
의 폭 차트를 설정 한 표시

# Do row labels 
if rowLabels is not None: 
    for row in xrange(rows): 
     table.add_cell(row + offset, -1, 
         width=rowLabelWidth or 1e-15, height=height, 
         text=rowLabels[row], facecolor=rowColours[row], 
         loc=rowLoc) 
    if rowLabelWidth == 0: 
     table.auto_set_column_width(-1) 

rowLabelWidth가 자동 크기로 설정되어 실제로 도움이되지 않습니다. 그러나 다음 세 가지 colWidth 옵션을 가지고 놀면 원하는 방식으로 작동하는 방법을 알아낼 수 있습니다. bbox 속성을 추가하여 테이블의 위치를 ​​명시하십시오. 주목해야 할 중요한 점은 rowLabelWidth가 전체 테이블 너비에 포함되어 있지 않다는 것입니다.

왼쪽에 튀어 나오지 않고 차트 아래에 rowLabel이 맞도록하려면 bbox 좌표를 사용하십시오. 테이블의 왼쪽 하단 모서리를 참조 점으로 사용하여 표를 배치합니다. bbox = [x_coordinate, y_coordinate, width, height]. x_coordinate가 tb_colWidth를 고려하여 설정되면 rowLabels-column의 가장 왼쪽면이 위의 차트의 왼쪽 구석 바로 아래에 오도록 필요한 정확한 양만큼 테이블을 이동시킵니다. 이 지금 차트 아래에서 돌출하는 가장 오른쪽 열을 발생하면

bbox = [tb_colWidth, y_coordinate, tb_width, tb_height] 

는 다음 하나 개의 컬럼의 크기로 폭을 축소 :

bbox = [tb_colWidth, y_coordinate, tb_width - tb_colWidth, tb_height] 

을 이제 모두 함께 넣어 :

the_table=plt.table(cellText = data, 
      rowLabels = rowLabels, rowColours = rowColours, 
      colLabels = colLabels, loc = 'bottom', 
      colWidths = colWidths, bbox = bbox) 

셀 너비를 조정하는 마지막 테이블 셀 루프를 수행 할 필요는 없지만 위의 도구를 사용하여 셀 너비를 제어 할 수 있습니다.