0

I'm trying to write a multicolored text in a legend
I.e currently the legend text looks as:
enter image description here
But I would like it to look as
enter image description here
I tried using the multicolored title example from matplotlib, But I think it only suits axis texts.

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 Answers1

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')

enter image description here