Python 在matplotlib图例中给文本加下划线

Python 在matplotlib图例中给文本加下划线,python,matplotlib,legend,underline,Python,Matplotlib,Legend,Underline,我需要在我的图例中加一点下划线。我发现一个问题得到了回答 但我一点也不懂乳胶。我需要在图例(代码第53行)中的“根据伽马能谱测定的含量”下划线。我尝试从链接执行以下操作: r'\underline{Content determined from gamma spectroscopy} ', 但确切的文字只是出现在传说中。我怎样才能在课文下面加下划线 import matplotlib.pyplot as plt from matplotlib.patches import Rectangle

我需要在我的图例中加一点下划线。我发现一个问题得到了回答 但我一点也不懂乳胶。我需要在图例(代码第53行)中的“根据伽马能谱测定的含量”下划线。我尝试从链接执行以下操作:

r'\underline{Content determined from gamma spectroscopy} ',
但确切的文字只是出现在传说中。我怎样才能在课文下面加下划线

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()
编辑:

我在评论中添加了以下内容以打开LaTeX

rc('text', usetex=True)
但这会导致一系列错误,现在以以下错误结束:

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

我猜这是因为我现在必须使用LaTeX格式化所有文本。有没有办法只使用LaTex格式化其中的一部分。我只是没有这方面的经验,现在真的不是学习它的最佳时机(不过总有一天我会的)。

您需要激活LaTeX渲染:

plt.rc('text', usetex=True)
请参见此处有关LaTeX渲染的详细信息:

关于您对“LaTeX无法处理以下字符串”的编辑。
您不需要以某种方式重新格式化所有文本。纯文本已经是LaTeX格式,您可以从错误消息中看到,matplotlib自动将数值转换为LaTeX格式(例如,
1e-4
转换为
$10^{-4}$


我认为问题在于缺少一些包裹。如果您在Ubuntu上,请确保安装了
dvipng
texlive latex extra
texlive math extra
软件包。

因为您忽略了

 from matplotlib import rc

 rc('text', usetex=True)
答案的一部分。LaTeX是一种标记语言,实际上是一种非常高级的语言


如果您不想深入了解这一点,您可能只需要使用matplotlib参数设置图例的文本样式,但它会将其应用于图例中的所有内容。

如果添加该参数,则会出现以下错误:RuntimeError:LaTeX无法处理以下字符串:'$10^{-4}$这是LaTeX生成的完整报告:报告中没有生成任何内容。我猜这是因为所有的文本都必须用LaTeX格式化,我不知道该怎么做。你的操作系统是什么?看来你少了一些乳胶包。