【问题标题】:Matplotlib Text Alignment in TableMatplotlib 表格中的文本对齐方式
【发布时间】:2019-07-09 22:56:44
【问题描述】:

我正在努力正确左/右对齐文本,而不会浪费列中的空间。中心/左/右使用相同的列宽:

  1. Center aligned: 正确居中,ok
  2. Left aligned: 左边浪费了空间
  3. 即使省略 colWidths 作为表参数并使用我最喜欢的解决方案 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()

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    默认情况下,单元格的两边都有 10% 的填充,这对于让文本在表格单元格边框旁边开始一点很有用。对于非常大的电池,10% 就太多了,会导致不希望的大空间。

    为了克服这个问题,需要将相关列的填充设置为较低的值,因为对于所有其他列,实际上需要 10%。没有设置填充的内置选项,但可以通过循环列的单元格并更改相应的PAD 属性来操作它。

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

    这只会为第二列 (col=1) 生成 1% 的填充。

    【讨论】:

    • 感谢您的快速回复。让填充值取决于字体大小而不是列宽不是更好吗?
    • table 类在 matplotlib 中的编写方式,没有办法将填充更改为依赖于字体大小 - 当然除了子类化 table 类和 cell 类并重新实现各自的方法.你是在问我对这个设计选择的看法吗?
    • 也许值得在 matplotlib 中打开一个错误?从我的角度来看,存在一种解决方法(您的解决方案),但应该存在合理的默认值/算法,因此用户不必调整实现内部 - 您的意见是什么?
    • 这是您的选择。我不会将其称为错误报告而是功能请求,但您可以将其提交至the matplotlib issue page。我个人认为这不是非常紧急或必要的,但这只是我的观点,其他人可能会有不同的看法。
    【解决方案2】:

    相当于 ImportanceOfBeingErnest 的回答,但语法更短(不是函数):

     for key,cell in the_table.get_celld().items():
        if key[1]==1:                   
            cell.PAD = 0.01
    

    代码获取 the_table 的第二列并更改填充。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-16
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 2017-11-09
      相关资源
      最近更新 更多