Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underlining text in matplotlib legend

I need to have a bit of text underlined in my legend. I found a question answered here but I do not understand LaTeX at all. I need to underline "Content determined from gamma spectroscopy" in the legend (line 53 of the code). I tried to do the following from the link:

r'\underline{Content determined from gamma spectroscopy} ',

but that exact text just shows up in the legend. How exactly can I underline the text?

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

eu_cl = 0.1
co_cl = 2.0

ca_eu_gs = 0.05 / eu_cl
ca_co_gs = 0.46 / co_cl
fa_eu_gs = 0.11 / eu_cl
fa_co_gs = 0.76 / co_cl
ce_eu_gs = 0.03 / eu_cl
ce_co_gs = 0.26 / co_cl

ca_eu_ms = 0.04 / eu_cl
ca_co_ms = 1.05 / co_cl
fa_eu_ms = 0.01 / eu_cl
fa_co_ms = 1.85 / co_cl
ce_eu_ms = 0.08 / eu_cl
ce_co_ms = 1.44 / co_cl

y_co = [1,1,1,1,1,1,1e-1,1e-2,1e-3,0]
x_co = [0,1e-4,1e-3,1e-2,1e-1,1e0,1e0,1e0,1e0,1e0]
#y_eu = [0, 1e-3, 1e-2, 1e-1, 1e0]
#x_eu = [1,1,1,1,1] 

plt.rcParams['legend.loc'] = 'best'
plt.figure(1)
plt.ylim(1e-3, 1e4)
plt.xlim(1e-4, 1e3)
#plt.autoscale(enable=True, axis='y', tight=None)
#plt.autoscale(enable=True, axis='x', tight=None)
ca_gs = plt.scatter(ca_eu_gs, ca_co_gs, color='b', marker='o')

fa_gs = plt.scatter(fa_eu_gs, fa_co_gs, color='r', marker='o')

ce_gs = plt.scatter(ce_eu_gs, ce_co_gs, color='m', marker='o')

ca_ms = plt.scatter(ca_eu_ms, ca_co_ms, color='b', marker='^')

fa_ms = plt.scatter(fa_eu_ms, fa_co_ms, color='r', marker='^')

ce_ms = plt.scatter(ce_eu_ms, ce_co_ms, color='m', marker='^')

extra = Rectangle((0, 0), 1, 1, fc="w", fill=False, edgecolor='none', linewidth=0)
extra1 = Rectangle((0, 0), 1, 1, fc="w", fill=False, edgecolor='none', linewidth=0)

clearance, = plt.plot(x_co, y_co, color='g')
#plt.plot(x_eu, y_eu, color='g')

plt.loglog()
plt.xlabel('Europium (ppm)')
plt.ylabel('Cobalt (ppm)')
plt.legend([extra, ca_gs, fa_gs, ce_gs, extra1, ca_ms, fa_ms, ce_ms , clearance], ("Content determined from gamma spectroscopy" ,
            "Coarse aggregate","Fine aggregate","Cement","Content determined from ICP-MS","Coarse aggregate","Fine aggregate","Cement","D/C = 1")
             , scatterpoints = 1)
print('plots created')
plt.show()

EDIT:

I added the following as mentioned in the comments to turn on LaTeX

rc('text', usetex=True)

But that results in a whole string of errors ending with the following error now:

RuntimeError: LaTeX was not able to process the following string:
'$10^{-4}$'
Here is the full report generated by LaTeX: 

I am guessing this is because I have to format all of my text using LaTeX now. Is there way to only format some of it using LaTex. I just have no experience with it and now really isn't the best time to learn it (I will someday though).

like image 404
Bogdan Janiszewski Avatar asked Dec 11 '25 06:12

Bogdan Janiszewski


2 Answers

It didn't work because you left out the

 from matplotlib import rc

 rc('text', usetex=True)

portion of the answer. LaTeX is a markup language, a very advanced one actually.

If you don't want to dive into that, you can probably just set the text style for the legend using the matplotlib parameter, but it will apply it to everything in the legend.

like image 53
aruisdante Avatar answered Dec 13 '25 20:12

aruisdante


You need to activate LaTeX rendering:

plt.rc('text', usetex=True)

See more information about LaTeX rendering here: http://matplotlib.org/users/usetex.html

Regarding your edit about "LaTeX was not able to process the following string".
You don't need to somehow reformat all your text. Plain text is already in LaTeX format, and as you can see from the error message, matplotlib automatically converts numeric values to LaTeX format (e.g. 1e-4 to $10^{-4}$).

I think the problem is some missing packages. If you are on Ubuntu, be sure to have dvipng, texlive-latex-extra and texlive-math-extra packages installed.

like image 32
max taldykin Avatar answered Dec 13 '25 20:12

max taldykin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!