Python 文本周围边框的间隙

Python 文本周围边框的间隙,python,matplotlib,Python,Matplotlib,我想在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", col

我想在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个符号显示间隙。


我使用的是matplotlib 1.5.1。

文档中没有提到它(或者我没有找到它),但是在代码中搜索时,我们可以看到
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”值似乎可以解决问题。 考虑下面的代码…< /P>
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")
。。。它产生了以下结果:



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

我认为这是有意的。字母的轮廓是用一个形状的画笔画出来的,从起点到终点的角度不同,因此出现了错误连接。这很好,谢谢!我猜这应该更好地记录在某个地方,可能在文档字符串中。我将对GitHub matplotlib存储库提出一个问题。