tex周围的边界间隙

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

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

我想在matplotlib绘图中的一些文本周围添加一个边框,我可以使用patheffects.withStroke。但是,对于某些字母和数字,符号右上角有一个小间隙。你知道吗

有没有办法避免这种差距?你知道吗

最小工作示例:

import matplotlib.pyplot as plt
import matplotlib.patheffects as patheffects

fig, ax = plt.subplots()
ax.text(
    0.1, 0.5, "test: S6",
    color='white',
    fontsize=90,
    path_effects=[patheffects.withStroke(linewidth=13, foreground='black')])
fig.savefig("text_stroke.png")

这给出了一个图像,它显示了S和6个符号之间的间隙。 enter image description here

我正在使用matplotlib 1.5.1。你知道吗


Tags: text文本import绘图matplotlibas字母fig
1条回答
网友
1楼 · 发布于 2024-10-02 02:42:32

文档没有提到它(或者我没有找到它),但是在代码中搜索时,我们可以看到patheffects.withStroke方法接受许多关键字参数。你知道吗

通过在交互式会话中执行以下命令,可以获得这些关键字参数的列表:

>>> from matplotlib.backend_bases import GraphicsContextBase as gcb
>>> print([attr[4:] for attr in dir(gcb) if attr.startswith("set_")])
['alpha', 'antialiased', 'capstyle', 'clip_path', 'clip_rectangle', 'dashes', 'foreground', 'gid', 'graylevel', 'hatch', 'joinstyle', 'linestyle', 'linewidth', 'sketch_params', 'snap', 'url']

您要查找的参数是capstyle,它接受3个可能的值:

  • “屁股”
  • “圆形”
  • “投射”

在您的例子中,“round”值似乎解决了问题。 考虑下面的代码。。。你知道吗

import matplotlib.pyplot as plt
import matplotlib.patheffects as patheffects

fig, ax = plt.subplots()
ax.text(
    0.1, 0.5, "test: S6",
    color='white',
    fontsize=90,
    path_effects=[patheffects.withStroke(linewidth=13, foreground='black', capstyle="round")])
fig.savefig("text_stroke.png")

。。。它产生了:

enter image description here


接受的关键字参数实际上是GraphicsContextBase类的所有set_*(减去“set”前缀)方法。您可以在类文档中找到接受值的详细信息。你知道吗

相关问题 更多 >

    热门问题