首页 > 解决方案 > MatplotLib:一次格式化多个单元格

问题描述

这是我的 matplotlib 函数,用于格式化单元格的行或列:

def setRowWiseFont(table, rowIndices, fontWeight = None, fontColor = None, fontSize = None, fontAlignment = None):
# Font Size Ranges 6 - 72 pts. Including medium as string
#Alignments =  'center' | 'right' | 'left'
if fontAlignment == None:
    fontAlignment = "center"
if fontColor == None:
    fontColor = "black"
if fontSize == None:
    fontSize = 6
if fontWeight == None:
    fontWeight = "normal"

for (row, col), cell in table.get_celld().items():

    if (row in rowIndices):
        cell.set_text_props(fontproperties=FontProperties(weight=fontWeight, size = fontSize),ha = fontAlignment)
        cell.get_text().set_color(fontColor)

return table

def setColumnWiseFont(table, colIndices, fontWeight = None, fontColor = None, fontSize = None, fontAlignment = None):

# Font Size Ranges 6 - 72 pts. Including medium as string
#Alignments =  'center' | 'right' | 'left'
if fontAlignment == None:
    fontAlignment = "center"
if fontColor == None:
    fontColor = "black"
if fontSize == None:
    fontSize = 6
if fontWeight == None:
    fontWeight = "normal"

for (row, col), cell in table.get_celld().items():

    if (col in colIndices):
        cell.set_text_props(fontproperties=FontProperties(weight=fontWeight, size = fontSize),ha = fontAlignment)
        cell.get_text().set_color(fontColor)

plt.show()
return table

如您所见,我可以使用上述两个函数格式化多行和多列,但从性能方面来说,这个函数很慢。我对可以让我通过索引或其他东西一次格式化多个单元格的东西感兴趣。例如:

cell[0:3,4:6].format(<formatting parms here>)

有谁知道这样做的方法?

标签: matplotlib

解决方案


推荐阅读