7

在此处输入图像描述

我对这张表有一个明显的问题,因为行标签在图之外,我不知道如何修复它。我知道我可以进入儿童艺术家并在那里更改高度和宽度之类的东西,但我已经尝试过但它没有用,所以也许你现在可以帮助我。

这是我为此使用的代码,希望它不会太难阅读......:

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()
4

2 回答 2

2

我还没有找到完美的答案,但我找到了一个对我自己的目的有用的解决方案。

同时调整 colWidth 和 table 宽度可以缩小 rowLabel 列的宽度。在源代码中,实际上他们使用了一个变量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)

这是 rowLabelWidth 的源代码中的内容,我们将使用它来帮助确定将 width 和 colWidth 设置为什么。

# 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)

看来您已将图表的宽度设置为轴([0.2, 0.45, 0.7, 0.45]),设置为 0.7,因此我们将其设置为变量 tb_width

tb_width = 0.7

rowLabelWidth 自动调整大小,这对我们没有帮助。但是,如果您尝试使用 colWidths 的以下三个选项,您可以开始弄清楚如何使其以您想要的方式工作。添加 bbox 属性以明确说明表格的位置。需要注意的重要一点是,rowLabelWidth 似乎不包括在整个表格宽度中。

## standard - essentially what happens when you don't specify colWidths. Takes the table width, divides it by the number of columns, giving each column an equal width.
colWidths = [tb_width/n_cols] * n_cols
## rowLabels stick out on the left

## similar to the above, but the '+1' attempts to account for the fact that another column's width, rowLabels, should fit inside the overall table width
colWidths=[( tb_width / (n_cols + 1) )] * n_cols  

## set your own width. this will start messing with the width of the rowLabelsWidth as now the colWidths aren't perfectly proportioned within the table width
tb_colWidth = 0.08
colWidths = [tb_colWidth] * n_cols

为确保 rowLabels 正好适合图表下方而不会突出到左侧,请使用 bbox 坐标。使用表格的左下角作为参考点,定位表格:bbox=[x_coordinate, y_coordinate, width, height]。如果 x_coordinate 设置为考虑您的 tb_colWidth,则它将表格移动到使 rowLabels 列的最左侧直接位于上图左下角所需的确切数量上。

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)

您不必执行调整单元格宽度的最终表格单元格循环,而是可以使用上述工具进行控制。

于 2017-11-14T18:54:33.443 回答
0

我遇到了这个问题,我通过将行标签的对齐方式从“左”更改为“右”来部分修复它。

cellDict = the_table.get_celld()
for x in range(1, len(row_labels)+1):
    cellDict[(x,-1)]._loc = 'right'

get_celld() 返回单元格的字典, cellDict[(x,-1)] 选择 x 行和 -1 列中的单元格,即行标签列。

这不会更改行标签列的宽度,因此标签左侧可能仍有一个空格。我的离开屏幕左侧,但至少我现在可以看到行标签中的所有文本。

于 2018-07-23T17:53:41.190 回答