简体   繁体   English

如何将matplotlib图重新缩放到文本?

[英]How to rescale matplotlib figure to text?

I would like to be able to autoscale a matplotlib figure to make arbitrarily placed text annotations visible. 我希望能够自动缩放matplotlib图形,以使任意放置的文本注释可见。 This is a challenge since artists other than Patches and Lines are not included in relim. 这是一个挑战,因为Patches和Lines以外的艺术家不包括在relim中。 For instance, the following text is out of the viewing area. 例如,以下文本不在查看区域中。

fig = figure()
ax = fig.add_subplot(111)
t = matplotlib.text.Text(10,10, 'Hello')
ax.add_artist(t)
show()

The docs cryptically mention using update_datalim_numerix , but how do I determine the correct bounding box in data units? 文档使用update_datalim_numerix 密码提及 ,但如何在数据单元中确定正确的边界框? Is there a method that will tell me the text bounding box size? 有没有一种方法可以告诉我文本边界框的大小? How do I cope with an artist that has been subjected to multiple transformations? 我如何应对经历过多次转换的艺术家?

You can place your text relative to the axis, as explained here : 您可以将文本相对于轴的地方,如解释在这里

import matplotlib.pyplot as plt
import matplotlib.text as txt

fig = plt.figure()
ax = fig.add_subplot(111)
t = txt.Text(0.5,0.5, 'Hello',transform=ax.transAxes)
ax.add_artist(t)
plt.show()

This example will place your text always at the center of the plot, no matter how you change the axis. 无论您如何更改轴,此示例都会将文本始终放在绘图的中心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM