matplotlib表中的粗体文本

时间:2018-12-11 15:02:38

标签: python matplotlib

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

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

2 个答案:

答案 0 :(得分: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')

给你

enter image description here

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

答案 1 :(得分:0)

docs中有一个示例,说明如何遍历表的单元格并应用字体属性。
要使第一行的单元格中的文本加粗,可以执行以下操作:

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'))
相关问题