在matplotlib中自动放置文本框 [英] automatically position text box in matplotlib
本文介绍了在matplotlib中自动放置文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以像使用pyplot.legend()那样告诉pyplot.text()?
Is there a way of telling pyplot.text() a location like you can with pyplot.legend()?
像图例这样的东西会很棒:
Something like the legend argument would be excellent:
plt.legend(loc="upper left")
我正在尝试使用字母(例如"A","B")标记具有不同轴的子图.我认为有一种比手动估算位置更好的方法.
I am trying to label subplots with different axes using letters (e.g. "A","B"). I figure there's got to be a better way than manually estimating the position.
谢谢
推荐答案
我不确定在最初发布问题时是否可用,但是现在可以实际使用loc参数了.下面是一个示例:
I'm not sure if this was available when I originally posted the question but using the loc parameter can now actually be used. Below is an example:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredText
# make some data
x = np.arange(10)
y = x
# set up figure and axes
f, ax = plt.subplots(1,1)
# loc works the same as it does with figures (though best doesn't work)
# pad=5 will increase the size of padding between the border and text
# borderpad=5 will increase the distance between the border and the axes
# frameon=False will remove the box around the text
anchored_text = AnchoredText("Test", loc=2)
ax.plot(x,y)
ax.add_artist(anchored_text)
plt.show()
这篇关于在matplotlib中自动放置文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文