matplotlib无法渲染乳胶表

发布于 2025-01-30 17:19:13 字数 1415 浏览 1 评论 0原文

我试图将此表添加到一个图中:

”“在此处输入图像说明”

示例脚本:

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['text.usetex'] = True

fig, ax = plt.subplots()

x = np.linspace(-10, 10)
ax.plot(x, x**2)

# adding table
table = r"""\begin{tabular}{cc}
\bf{Material} & \bf{Roughness ($\epsilon$)} \\
\midrule
Drawn Tubing & 0.000005 \\
Commercial Steel or Wrought Iron & 0.00015 \\
Asphalted Cast Iron & 0.0004 \\
Galvanized Iron & 0.0005 \\
Wood Stave & 0.0006-0.003 \\
Cast Iron & 0.00085 \\
Concrete & 0.001-0.01 \\
Riveted Steel & 0.003-0.03
\end{tabular}"""

ax.annotate(table, xy=(0, 60), ha='center', va='center')

plt.show()

上面的乳胶语法是正确的,因为我在上面的图像中获得了成功的输出。上面的脚本失败了,乳胶无法注册存储在Table的字符串。如果我将字符串替换为r'\ bf {testing}',则脚本将运行并输出以下内容:

”

我迷路了,因为它是上述示例显示Matplotlib能够处理乳胶,但不能处理表格环境。我对乳胶不太熟悉,但是我想知道Matplotlib是否不会自动为序言添加一些依赖性。此示例类似于 a>,但它仍然行不通。

I am trying to add this table to a plot:

enter image description here

Example Script:

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['text.usetex'] = True

fig, ax = plt.subplots()

x = np.linspace(-10, 10)
ax.plot(x, x**2)

# adding table
table = r"""\begin{tabular}{cc}
\bf{Material} & \bf{Roughness ($\epsilon$)} \\
\midrule
Drawn Tubing & 0.000005 \\
Commercial Steel or Wrought Iron & 0.00015 \\
Asphalted Cast Iron & 0.0004 \\
Galvanized Iron & 0.0005 \\
Wood Stave & 0.0006-0.003 \\
Cast Iron & 0.00085 \\
Concrete & 0.001-0.01 \\
Riveted Steel & 0.003-0.03
\end{tabular}"""

ax.annotate(table, xy=(0, 60), ha='center', va='center')

plt.show()

The LaTeX syntax above is correct because I got the successful output in the above image. The above script fails with LaTeX not able to register the string stored in table. If I were to replace the string with r'\bf{Testing}', the script will run and output the following:

enter image description here

I'm lost because it the above example shows that matplotlib is able to handle the LaTeX, but not the tabular environment. I'm not too familiar with LaTeX, but I'm wondering if matplotlib doesn't automatically add some dependency to the preamble. This example is similar to this SO post, but it still won't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2025-02-06 17:19:13

该代码正在创建一个多行字符串(在每行末尾添加\ n)。如果您运行print(reper(table)),则输出是:

'\\begin{tabular}{cc}\n\\bf{Material} & \\bf{Roughness ($\\epsilon$)} \\\\\n\\midrule\nDrawn Tubing & 0.000005 \\\\\nCommercial Steel or Wrought Iron & 0.00015 \\\\\nAsphalted Cast Iron & 0.0004 \\\\\nGalvanized Iron & 0.0005 \\\\\nWood Stave & 0.0006-0.003 \\\\\nCast Iron & 0.00085 \\\\\nConcrete & 0.001-0.01 \\\\\nRiveted Steel & 0.003-0.03\n\\end{tabular}'

您可以使用()

table = (r"\begin{tabular}{cc}"
r"\bf{Material} & \bf{Roughness ($\epsilon$)} \\"
r"\hline "
r"Drawn Tubing & 0.000005 \\"
r"Commercial Steel or Wrought Iron & 0.00015 \\"
r"Asphalted Cast Iron & 0.0004 \\"
r"Galvanized Iron & 0.0005 \\"
r"Wood Stave & 0.0006-0.003 \\"
r"Cast Iron & 0.00085 \\"
r"Concrete & 0.001-0.01 \\"
r"Riveted Steel & 0.003-0.03"
r"\end{tabular}")

将领先的“ R”放在每个上行,以便将所有部分解释为RAW(请参见在这里)。

此外,由于未识别,我必须替换r“ \ hline”(包装?)。

The code is creating a multi line string (adding \n at the end of each line). If you run print(repr(table)), the output is:

'\\begin{tabular}{cc}\n\\bf{Material} & \\bf{Roughness ($\\epsilon$)} \\\\\n\\midrule\nDrawn Tubing & 0.000005 \\\\\nCommercial Steel or Wrought Iron & 0.00015 \\\\\nAsphalted Cast Iron & 0.0004 \\\\\nGalvanized Iron & 0.0005 \\\\\nWood Stave & 0.0006-0.003 \\\\\nCast Iron & 0.00085 \\\\\nConcrete & 0.001-0.01 \\\\\nRiveted Steel & 0.003-0.03\n\\end{tabular}'

You can construct a long raw line by using the ():

table = (r"\begin{tabular}{cc}"
r"\bf{Material} & \bf{Roughness ($\epsilon$)} \\"
r"\hline "
r"Drawn Tubing & 0.000005 \\"
r"Commercial Steel or Wrought Iron & 0.00015 \\"
r"Asphalted Cast Iron & 0.0004 \\"
r"Galvanized Iron & 0.0005 \\"
r"Wood Stave & 0.0006-0.003 \\"
r"Cast Iron & 0.00085 \\"
r"Concrete & 0.001-0.01 \\"
r"Riveted Steel & 0.003-0.03"
r"\end{tabular}")

Putting the leading "r" at every line, so that all portions are interpreted as raw (see here).

Furthermore, I had to replace the midrule by r"\hline ", as it was not recognised (package required?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文