【问题标题】:matplotlib scale text (curly brackets)matplotlib 缩放文本(大括号)
【发布时间】:2018-04-26 09:38:00
【问题描述】:

我想在我的图中使用大括号“}”,它们都具有不同的高度,但宽度相同。 到目前为止,缩放文本时,宽度按比例缩放:

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()

我想到的唯一想法是用 matplotlib 图像覆盖大括号图像,例如使用svgutils 就像在Importing an svg file a matplotlib figure 中一样,但这很麻烦。

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

【问题讨论】:

  • 我确实认为使用LaTeX 文本渲染和\scalebox 可以在这里完成这项工作,但似乎doesn't work

标签: python matplotlib svg


【解决方案1】:

要让一个字母只在一维上缩放,例如高度但保持其他尺寸不变,您可以将大括号创建为TextPath。这可以作为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()

【讨论】:

  • 是否可以调整此函数以在图形外部绘制括号?比如右边?
  • @jonboy 当然,您只需将clip_path 设置为关闭。
  • 干杯。谢谢@ImportanceOfBeingErnest
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-10
  • 1970-01-01
  • 2017-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多