【问题标题】:bold text in matplotlib tablematplotlib 表中的粗体文本
【发布时间】:2019-05-12 14:31:46
【问题描述】:

如何在 matplotlib 表格(pyplot 表格)的单元格中插入粗体文本?

import matplotlib.pyplot as plt
plt.table(cellText=[['my_texte_bold', 'other cell'], ['cell1', 'cell2'])

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    请参阅documentation,了解如何迭代表格单元格并应用字体属性的示例。例如,要将第一行中的文本加粗,您可以执行以下操作:

    import matplotlib.pyplot as plt
    from matplotlib.font_manager import FontProperties
    
    table = plt.table(cellText=[['my_texte_bold', 'other cell'], ['cell1', 'cell2']])
    for (row, col), cell in table.get_celld().items():
        if (row == 0):
            cell.set_text_props(fontproperties=FontProperties(weight='bold'))
    

    【讨论】:

    【解决方案2】:

    您可以在 matplotlib 文本中使用 tex 代码。这样你也可以混合不同的字体属性

    import matplotlib.pyplot as plt
    t=plt.table(cellText=[['my_texte_$\\bf{bold}$', 'other cell'], ['cell1', 'cell2']])
    t.scale(1,7)
    plt.axis('off')
    

    给你

    注意: 您可能会遇到粗体区域中空格丢失的问题。 在这种情况下使用strBold.replace(' ','\\ ')。这会在每个空格前面放置一个'\\'

    另见: https://stackoverflow.com/a/61538854/7128154 以获取基于 plt.text 示例的更多选项

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      • 2011-03-09
      相关资源
      最近更新 更多