Home > OS >  matplotlib text alignment when using latex
matplotlib text alignment when using latex

Time:12-04

Before, this code:

import matplotlib.pyplot as plt

plt.rcParams.update({
    "text.usetex": True})

plt.plot([0, 1], [0, 1])
plt.xlabel('hello\nnew world')

generated a plot with centered alignment in the x label, something like here: enter image description here

But now I get this left aligned text:

enter image description here

I am not sure if matplotlib or latex version changed, but wondering how should I adapt my code to get back to the usual center alignment.

Thank you!

Related question: enter image description here

\n can be replaced by \\\ or \\newline (as an additional \ is needed as escape character) or \linebreak (so that it doesn't start a new paragraph).

Another option is to put the centering locally:

import matplotlib.pyplot as plt

plt.rcParams.update({
    "text.usetex": True})

plt.figure(figsize=(2,2))
plt.plot([0, 1], [0, 1])
plt.xlabel('\\begin{center} hello \linebreak new world \end{center}')

enter image description here

  • Related