matplotlib 比例文本(大括号)

giz*_*ole 3 python svg matplotlib

我想在我的情节中使用大括号 '}',所有这些都具有不同的高度,但宽度相同。到目前为止,缩放文本时,宽度按比例缩放:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ax.text(0.2, 0.2, '}', fontsize=20)
ax.text(0.4, 0.2, '}', fontsize=40)
plt.show()
Run Code Online (Sandbox Code Playgroud)

我想到的唯一想法是用 matplotlib 图像覆盖大括号的图像,例如使用svgutils导入 svg 文件中的 matplotlib 图形,但这很麻烦。

将矢量图形作为输出的解决方案将是理想的。

Imp*_*est 6

要仅在一个维度上缩放字母,例如高度但保持另一个维度不变,您可以将大括号创建为TextPath。这可以作为 a 的输入提供PathPatch。并且PathPatch可以使用 任意缩放matplotlib.transforms

import matplotlib.transforms as mtrans
from matplotlib.text import TextPath
from matplotlib.patches import PathPatch

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

def curly(x,y, scale, ax=None):
    if not ax: ax=plt.gca()
    tp = TextPath((0, 0), "}", size=1)
    trans = mtrans.Affine2D().scale(1, scale) + \
        mtrans.Affine2D().translate(x,y) + ax.transData
    pp = PathPatch(tp, lw=0, fc="k", transform=trans)
    ax.add_artist(pp)

X = [0,1,2,3,4]
Y = [1,1,2,2,3]
S = [1,2,3,4,1]

for x,y,s in zip(X,Y,S):
    curly(x,y,s, ax=ax)

ax.axis([0,5,0,7])
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • @jonboy当然,你只需要关闭“clip_path”即可。 (2认同)