【问题标题】:Underlining text in matplotlib legendmatplotlib 图例中的下划线文本
【发布时间】:2014-04-12 03:09:41
【问题描述】:

我需要在我的图例中添加一些文字下划线。我发现一个问题已回答here 但我根本不懂 LaTeX。我需要在图例(代码的第 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()

编辑:

我在 cmets 中添加了以下内容以打开 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 格式化其中的一部分。我只是没有这方面的经验,现在真的不是学习它的最佳时间(我总有一天会的)。

【问题讨论】:

  • 你的操作系统是什么?似乎您缺少一些乳胶包。

标签: python matplotlib legend underline


【解决方案1】:

没用,因为你漏掉了

 from matplotlib import rc

 rc('text', usetex=True)

部分答案。 LaTeX 是一种标记语言,实际上是一种非常先进的语言。

如果您不想深入研究,您可以只使用 matplotlib 参数设置图例的文本样式,但它会将其应用于图例中的所有内容。

【讨论】:

  • 如果我添加它,我会收到以下错误: RuntimeError: LaTeX was not able to process the following string: '$10^{-4}$' 这是 LaTeX 生成的完整报告:报告中没有生成任何内容。我猜这是因为所有文本都必须使用 LaTeX 进行格式化,而我并不完全知道该怎么做。
【解决方案2】:

你需要激活 LaTeX 渲染:

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

在此处查看有关 LaTeX 渲染的更多信息:http://matplotlib.org/users/usetex.html

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

我认为问题在于缺少一些软件包。如果您使用的是 Ubuntu,请务必安装 dvipngtexlive-latex-extratexlive-math-extra 软件包。

【讨论】:

    猜你喜欢
    • 2018-02-11
    • 2012-05-30
    • 2015-05-24
    • 2012-02-21
    • 2013-08-22
    • 1970-01-01
    • 2014-07-22
    • 2011-02-07
    相关资源
    最近更新 更多