将普通字符串转换为可在matplotlib中使用的乳胶字符串 [英] convert normal string to latex string to use in matplotlib

查看:101
本文介绍了将普通字符串转换为可在matplotlib中使用的乳胶字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道,如果我想在绘图中使用LaTeX字符串,则应该代替"sin(x)",而应该使用r"\sin(x)".

So I know that if I want to use a LaTeX string in my plots I should instead of for example "sin(x)", I should use r"\sin(x)".

但是,如果我有a = "\sin(x)",并且现在想将此a用作绘图标签,如何将其转换为r"\sin(x)"?当我做type(r"\sin(x))"只是说字符串.

But if I have a = "\sin(x)" and I now want to use this a as my plot label, how can I convert it to r"\sin(x)"? When I do type(r"\sin(x))" is just says string.

推荐答案

请注意,要激活MathText,字符串必须位于美元符号($)之间.

Mind that to have MathText activated the string must be in between Dollar signs ($).

如果您的乳胶包含反斜杠,则需要从头开始使用原始字符串

In case your latex contains backslashes you need to either use a raw string from the beginning

a = r"$\tan(\nu\cdot x)$"

或转义反斜杠

a = "$\\tan(\\nu\\cdot x)$"

如果您尝试使用其他答案中的类似方法,则会得到意想不到的结果

If you try something like in the other answer, you'd get unexpected results

a = "\tan(\nu\cdot x)"
b = r"$"+a+"$"
ax.plot(x, y, label=b)

结果

这篇关于将普通字符串转换为可在matplotlib中使用的乳胶字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆