LaTeX color labels on charts

I am trying to make the x-tag color, but I have no idea how to set this. The difficulty is that I want to use the LaTeX font and the "amsmath" package.

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 pronounced colors?

Thank you in advance!

+4
source share
1 answer

I believe that the problem here may be in latex mode. Your command \textis the Latex command present in amsmath to set the text as such in the equations. He does not give any flowers.

Without trying it myself in Python, I would suggest you try loading xcolor into Latex:

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

xcolors '\color -command:

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

, Python, , \text :

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

, .

, !

+1

Source: https://habr.com/ru/post/1544233/


All Articles