I am creating a figure to be saved as PDF using matplotlib in Python. (Only) the first letter of the figure title needs to be bolded and a specific (custom) color. Minimalistically, I would assume the following would work (non-custom color):
fig,ax = plt.subplots(1)
ax.plot(1,1)
ax.set_title(r'{\bf\color{red} A}: absquatulate')
plt.savefig('test.pdf')
But it doesn't.
One can instead (I can't find the reference) go to much more trouble, using PGF backend, to get some color, but this fails when the r'\bf'
is used:
import matplotlib.pyplot as plt
pgf_with_latex = {
"text.usetex": True,
"pgf.rcfonts": False,
"pgf.preamble": [
r'\usepackage{color}',
r'\definecolor{colorB}{rgb}{ 0.1, 0.5, 0.999 }'
]
}
matplotlib.rcParams.update(pgf_with_latex)
fig,ax = plt.subplots(1)
ax.plot(1,1)
ax.set_title(r'{\bf\color{colorB} A}: absquatulate')
plt.savefig('test.pdf')
How do I get the \bf\color to work simultaneously?
Actually, the following works (Use \bfseries
, and I find it requires separate nested braces):
#!/usr/bin/python
import matplotlib
from matplotlib.backends.backend_pgf import FigureCanvasPgf
matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf)
pgf_with_latex = {
"text.usetex": True,
"pgf.preamble": [
r'\usepackage{color}',
r'\definecolor{colorB}{rgb}{ 0.1, 0.5, 0.999 }'
]
}
import matplotlib
matplotlib.rcParams.update(pgf_with_latex)
import matplotlib.pyplot as plt
plt.figure(figsize=(2,2))
plt.plot(1,1)
plt.title(r'{ \textcolor{colorB} {\bfseries A}}: absquatulate')
plt.savefig('test.pdf')
A slightly updated version from CPBL's elegant answer . The main difference is that newer versions of Matplotlib want to get pgf.preamble
as on string, not a list of strings. Also, seems that text.usetex
is already True
by default.
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pgf import FigureCanvasPgf
matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf)
matplotlib.rcParams.update({
"pgf.preamble": r"""
\usepackage{xcolor}
\definecolor{colorB}{rgb}{ 0.1, 0.5, 0.999 }
"""
})
plt.figure(figsize=(2, 2))
plt.plot(1, 1)
plt.title(r'{ \textcolor{colorB} {\bfseries A}}: absquatulate')
plt.savefig('test.pdf')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.