【问题标题】:Matplotlib Table Row Label Font Color and SizeMatplotlib 表格行标签字体颜色和大小
【发布时间】:2016-05-31 19:57:47
【问题描述】:

给定下表:

import matplotlib.pyplot as plt
table=plt.table(cellText=[' ', ' ', ' ', ' ', ' '], # rows of data values
          rowLabels=['1','2','3','4','5'],
          cellLoc="left",
          rowLoc='left',
          bbox=[0,0,.2,1], # [left,bottom,width,height]
          edges="")

我想将数字 (1-5) 的颜色更改为灰色,并将字体大小更改为 12 磅。

【问题讨论】:

    标签: python python-3.x matplotlib


    【解决方案1】:

    您需要获取单元格的文本字体属性:

    import matplotlib.pyplot as plt
    table=plt.table(cellText=[' ', ' ', ' ', ' ', ' '], #rows of data values
              rowLabels=['1','2','3','4','5'],
              cellLoc="left",
              rowLoc='left',
              bbox=[0,0,.2,1],#[left,bottom,width,height]
              edges="")
    
    # iterate through cells of a table
    table_props = table.properties()
    table_cells = table_props['child_artists']
    for cell in table_cells: 
            cell.get_text().set_fontsize(20)
            cell.get_text().set_color('grey')
    plt.show()
    

    获取单元格文本属性的另一种方法是使用单元格索引(i,j):

    table[(i, j)].get_text().set_fontsize(12)
    table[(i, j)].get_text().set_color('red')
    

    Matplotlib 文本字体属性在此处描述:http://matplotlib.org/api/text_api.html#matplotlib.text.Text.set_fontproperties

    结果,第一个代码绘制了这个图:

    【讨论】:

    • 谢谢,斯坦利。如果我的表是轴(即 ax1.table(cellText....) 怎么办?当我以这种方式尝试并使用 ax1.properties() 而不是 table.properties() 时,我得到“KeyError:'child_artists'”
    • 并非每个matpoltlib 对象都有§child_artists§ 方法。
    • @DanceParty2:应该仍然有效。换句话说,table = ax[1].table(cellText...),然后是table.properties()。我不能保证这适用于每个版本的 Matplotlib,但它适用于 v1.3.1。
    • 我在 'children' 而不是 'child_artists' 下找到了子单元格
    猜你喜欢
    • 1970-01-01
    • 2015-08-23
    • 2019-09-24
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 2020-02-18
    相关资源
    最近更新 更多