matplotlib 表中的粗体文本

Clé*_*t v 2 python matplotlib

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

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

Mar*_*hke 8

您可以在 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')
Run Code Online (Sandbox Code Playgroud)

给你

在此输入图像描述

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

另请参阅: https: //stackoverflow.com/a/61538854/7128154,了解基于plt.text示例的更多选项


Geo*_*rgy 6

有关如何迭代表格的单元格和应用字体属性的示例,请参阅文档。例如,要使第一行中的文本加粗,您可以执行以下操作:

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'))
Run Code Online (Sandbox Code Playgroud)