convert normal string to latex string to use in matplotlib(将普通字符串转换为乳胶字符串以在 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"$ an(
ucdot x)$"
或转义反斜杠
a = "$\tan(\nu\cdot x)$"
如果您尝试其他答案中的类似内容,您会得到意想不到的结果
If you try something like in the other answer, you'd get unexpected results
a = " an(
ucdot x)"
b = r"$"+a+"$"
ax.plot(x, y, label=b)
结果
这篇关于将普通字符串转换为乳胶字符串以在 matplotlib 中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将普通字符串转换为乳胶字符串以在 matplotlib 中使用


基础教程推荐
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01