简体   繁体   中英

bold text in matplotlib table

How to insert bold text in a cell of matplotlib table (pyplot table)?

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

See the documentation for an example of how to iterate over the cells of the table and apply font properties. For example, to make the text in the first row bold, you could do the following:

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'))

You can use tex code in matplotlib texts. In this way you can also mix different font properties

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

giving you

在此处输入图像描述

Note: You might face the problem that whitespaces are lost in the bold region. In this case use strBold.replace(' ','\\ ') . This puts a '\\' in front of every whitespace.

See also: https://stackoverflow.com/a/61538854/7128154 for more options based on an plt.text example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM