text,usetex=true设置将轴标签呈现为

2024-10-02 02:32:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我今天试着在图形中渲染一些纹理,但没用。我意识到,matplotlibrc文件中的text.usetex被设置为False。 当我将rc('text', usetex=True)添加到脚本中时,轴标签也呈现为TeX,这是不需要的。 我不记得在matplotlib 1.3.0之前设置过这个,而且我绝对不记得渲染TeX有什么困难

还有人经历过这种行为吗?在

示例:

import matplotlib.patheffects as PathEffects

# matplotlib.rc('text', usetex=True)

fig = plt.figure(figsize=(4,4))
ax = fig.add_axes([0,0,0.9,1])
ax.imshow(randn(20,20))
txt = ax.text(0.1, 0.5, r"Some \LaTeX\ $\alpha=\beta$", transform=ax.transAxes,fontsize=16)
txt.set_path_effects([PathEffects.Stroke(linewidth=3, foreground="w"), PathEffects.Normal()])

产生:

enter image description here

取消注释`matplotlib.rc文件('text',usetex=True)'行,生成:

enter image description here


Tags: 文件texttxttrue图形matplotlibfigax
1条回答
网友
1楼 · 发布于 2024-10-02 02:32:40

你的“问题”其实不是一个问题,但我假设你有以下问题:

您希望使用latex字体向绘图中添加一些文本,或者只添加数学而不使用rc('text', usetex=True)(请注意,在默认的rcparams文件中,它声明这会影响all文本)。在

可以这样做:

import matplotlib.pylab as plt

fig = plt.figure()
plt.annotate(r"$\mathcal{G}r \epsilon \epsilon \kappa$", xy=(5, 2), size=26)
plt.annotate(r"default font", xy=(2, 5), size=16)
plt.annotate(r"latex font", xy=(2, 7), size=20, family='computer modern roman')
plt.plot(range(10))
plt.xlabel("some string")

enter image description here

希望能有所帮助!在

相关问题 更多 >

    热门问题