如何将plt.text字段放在另一个上面?

问题描述 投票:0回答:1

我试图在相同的x坐标上添加几个文本行,一个在另一个上面。问题是y scale max是高的,所以如果我把它们放在另一个上面(因此第一个值y是第二个值+字体大小的y),它们非常接近以至于无法将它们彼此区分开来。一种解决方案是将间隙乘以比例。或者找出前一行使用的矩形并使用它计算y。但是,如果我放大图片,它们将不会彼此相邻......任何可扩展的解决方案,好吗?文本是以某种方式自动缩放的,是否有间隙/ y轴的选项?

x = <my calculated x value>
y = 6 * (n - i - 1) # where n - amount of lines, i - index of current text line
text = plt.text(x, y, text_drops, fontsize=6)

接下来的两张图片显示了该图的相同部分 - 实际尺寸和放大。

这个显示实际比例:一个单元格的高度为50000

Scaled so y cell is 25 points high

这是放大的,因此一个单元格的高度为25

Scaled so y cell is 25 points high

UPD:以其他方式将Joooeey的建议付诸行动 - 为我工作:

x = <my calculated x value>
text = plt.text(x, 0, text_drops + i * '\n', fontsize=6) # where i - index of current  line
python python-3.x matplotlib
1个回答
1
投票

您可以使用一个文本字段,以换行符分隔:

lines = ['drop start at 125989msg/sec',
         'drop start at 126169msg/sec',
         'drop start at 126381msg/sec']
multiline = '\n'.join(lines) # put in line breaks
texts = plt.text(x, y, multiline, horizontalalignment='left', verticalalignment='left')
© www.soinside.com 2019 - 2024. All rights reserved.