Home > OS >  x label in matplotlib using Latex wrapper: the space characther disappears
x label in matplotlib using Latex wrapper: the space characther disappears

Time:10-18

I want to set some part of the text of the x-axis bold and some other part normal font (not bold). I tried to use the Latex wrapper of matplotlib (which works) but the space character string xlabel bold (between xlabel and bold) disappears. How can I fix the problem?

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
#ax.set_xlabel(r'$\mathbf{xlabel bold}$ - '   r'xlable not bold')
ax.set_xlabel(r'$\bf{xlabel bold}$ - '   'xlable not bold')

plt.show()

plot

CodePudding user response:

Here you go:

ax.set_xlabel(r'$\bf{xlabel \ bold}$ - '   'xlable not bold')

Or you can use this:

ax.set_xlabel(r'$\bf{xlabel}$ $\bf{bold}$ - '   'xlable not bold')

Alternatively you can use a simple function:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

def bold_text(text):
    new_text = text.replace(' ', r'}$ $\bf{')
    new_text = r'$\bf{'  new_text   r'}$'
    return new_text

new_text = bold_text('this is some bold text')
ax.set_xlabel(new_text   ' - '   'xlable not bold')
plt.show()

For even more options you can go to: https://www.overleaf.com/learn/latex/Spacing_in_math_mode

  • Related