I'm trying to write a multicolored text in a legend
I.e currently the legend text looks as:
But I would like it to look as
I tried using the multicolored title example from matplotlib, But I think it only suits axis texts.
Asked
Active
Viewed 213 times
0

DsCpp
- 2,259
- 3
- 18
- 46
-
Yes, but the same strategy would need to be applied here, it will just be even more complicated. Though, depending on the application, you could also use latex as in [here](https://stackoverflow.com/questions/9169052/partial-coloring-of-text-in-matplotlib). – ImportanceOfBeingErnest Nov 05 '19 at 13:41
1 Answers
2
You can use LaTeX for this:
from matplotlib import rc, pyplot
import matplotlib
import numpy as np
matplotlib.rc('text', usetex=True)
matplotlib.rc('text.latex', preamble='\usepackage{color}')
matplotlib.use('ps')
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
fig, ax = pyplot.subplots()
ax.plot(t, s, label=r'\textcolor{red}{Hello} \textcolor{green}{color} \textcolor{blue}{legend}!')
pyplot.legend()
pyplot.savefig('test.ps')

Francisca Concha-Ramírez
- 912
- 2
- 10
- 25