3

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

Therese
  • 63
  • 1
  • 2
  • 6
  • [This question](https://stackoverflow.com/questions/34937048/make-part-of-a-matplotlib-title-bold-and-a-different-color?rq=1) seems to better suit the needs here. – ImportanceOfBeingErnest May 22 '17 at 23:07

1 Answers1

4

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

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thanks for the fast reply, unfortunately this code doesn't work for me. I copied it and checked it in ipython. My number comes out in latex-style, but not bold. I use the python version 2.7.6 and Tex Live 2013/Debian (3.1415926). (I am not sure where and how to upload the example plot of that). – Therese May 23 '17 at 16:19
  • Did you open a new IPython console and type **exactly** the code as above? Can you post a screenshot? – ImportanceOfBeingErnest May 23 '17 at 16:22
  • yes, I copied your code into a new ipython session. Just posted the screenshot with my post edit. – Therese May 23 '17 at 16:57
  • So my solution seems to only work with matplotlib version 2, while you seem to have a version below or equal 1.5. (a) Have you tried any of the other solution in the linked question? (b) Are you willing to update matplotlib? If none of (a) or (b) work for you, we can open up the question again, but that would require you to clearly state in the question that (a) and (b) are not working. – ImportanceOfBeingErnest May 23 '17 at 17:16
  • Thanks, that's good to know. I have tried the other solution using rc, that causes a RuntimeError: dvipng was not able to process the following file: /home/rieckh/.cache/matplotlib/tex.cache/c9720fd8a1daf78cb9f17adb94b78c75.dvi. I'll try to get matplotlib updated. Thanks for your help! – Therese May 23 '17 at 17:25