1
votes

I would like to create an axis title that has part of it in bold font. For example, I am comparing two data sets and computing R^2, and if there is statistical significance, I would like to print R^2 bold into the title. So far I can only make the entire title bold.

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = plt.subplot()
# example R^2 
r1 = np.random.random()
ax1.set_title('part of title that should not be bold, $R^2$: {0}'.format(np.around(r1, 3)), weight='bold')

I only want the number after "R^2" to be bold.

Thanks!

EDIT:

The answer below suggests to use bold MathText, such as: r"$\bf{0.333}$". This results in Latex-style numbers, but it's still not bold. Here is the code from below pasted into a new ipython session, and the result:

Here is the code from below pasted into a new ipython session, and the result

1
This question seems to better suit the needs here. - ImportanceOfBeingErnest

1 Answers

4
votes

The following seems to only work on matplotlib version 2 or above.

One can use bold MathText inside the title to make part of the text bold, e.g. r"$\bf{0.333}$". Note that this is a raw string (r""). If we want to format the string with brackets, they have to be double escaped,

`r"$\bf{{{x}}}$".format(x=0.333)`

Complete example:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = plt.subplot()
# example R^2 
r1 = np.random.random()
ax1.set_title(r'non-bold part of title, $R^2$: $\bf{{{a}}}$'.format(a=np.around(r1, 3) ) )

plt.show()

enter image description here