Clément v
Clément v

Reputation: 123

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

Upvotes: 3

Views: 9129

Answers (2)

Georgy
Georgy

Reputation: 13717

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

Upvotes: 8

Markus Dutschke
Markus Dutschke

Reputation: 10606

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

enter image description here

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

Upvotes: 8

Related Questions