4

I am trying to make the x-label colored, but I have no idea how to establish that. The difficulty is that I want to use LaTeX font and the package "amsmath".

My code is:

import math
import numpy as np
import matplotlib.pyplot as plt


x,y=np.random.rand(20),np.random.rand(20)


plt.figure()

plt.rc('text', usetex=True)
plt.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]


plt.plot(x,y,'.')




plt.xlabel(r'$x=\frac{\text{red}}{\text{blue}}$')
plt.ylabel(r'$y$')

#plt.savefig('colored_labels.png')

plt.show()

enter image description here

Does anyone have a trick how to make the x-axis in the pronounced colors?

Thank you in advance!

Andy
  • 1,072
  • 2
  • 19
  • 33
  • Do you want the whole thing coloured some colour or do you want the "red" red and the "blue" blue? – Ffisegydd Jun 11 '14 at 21:59
  • I would like to have the "red" red and "blue" blue – Andy Jun 11 '14 at 22:15
  • 3
    Check this out: [question](http://stackoverflow.com/questions/9169052/partial-coloring-of-text-in-matplotlib) – The Dude Jun 11 '14 at 23:05
  • I looked at the questions you posted above already, the problem is here that I want to use Latex font.. and here I somehow can't manage colored labels – Andy Jun 12 '14 at 14:18
  • Look at the question linked by The Dude, it should answer your question. – Ffisegydd Jun 12 '14 at 14:24
  • 2
    When using Latex to show colors in fonts, whether or not the colors show up depends on the matplotlib backend. I don't know the survey of which backends work and which don't, but it would be useful if someone compiled a list of these since this question comes up frequently. – tom10 Jun 12 '14 at 14:44
  • 1
    I've looked at the question linked by The Dude, particularily the second answer there and now I tried to use the postscript-backend, but I get a "RuntimeError: ghostscript was not able to process your image .." – Andy Jun 12 '14 at 14:49

1 Answers1

1

I believe the problem here might be in the Latex regime. Your command \text is a Latex-command present in amsmath to set text as such in equations. It does not provide any colors.

Without having tried it myself in Python, I would suggest you try loading xcolor in Latex as well:

plt.rcParams['text.latex.preamble']=[r"\usepackage{xcolor}"]

Then you'll be able to use xcolors' \color-command:

plt.xlabel(r'$x=\frac{ \color{red}{red text} }{ \color{blue}{blue text} }$')

Again, untested in Python, but you might even get better results using the \text-command on top of this:

plt.xlabel(r'$x=\frac{ \text{\color{red}{red text}} }{ \text{\color{blue}{blue text}} }$')

At least the Latex-part of this code works and gives you the desired results.

Let me know if it worked for you!

Dux
  • 1,226
  • 10
  • 29
  • thanks, but it's still not colored... seems that python is not compatible with colored LaTeX fonts?! – Andy Jun 16 '14 at 21:26
  • It looks that way, yes. Based on a quick google search I would think it depends on the backend used, it might be possible to get colored fonts in ps-output. But I lost the source of that information. – Dux Jun 23 '14 at 12:33