Is there a way of telling pyplot.text() a location like you can with pyplot.legend()?
有没有办法告诉pyplot.text()像pyplot.legend()一样的位置?
Something like the legend argument would be excellent:
类似于传奇论点的东西会很棒:
plt.legend(loc="upper left")
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.
我试图用字母标记不同轴的子图(例如“A”,“B”)。我认为必须有一种比手动估算位置更好的方法。
Thanks
谢谢
2 个解决方案
#1
34
Just use annotate
and specify axis coordinates. For example, "upper left" would be:
只需使用注释并指定轴坐标。例如,“左上角”将是:
plt.annotate('Something', xy=(0.05, 0.95), xycoords='axes fraction')
You could also get fancier and specify a constant offset in points:
你也可以变得更漂亮,并指定一个点的常量偏移:
plt.annotate('Something', xy=(0, 1), xytext=(12, -12), va='top'
xycoords='axes fraction', textcoords='offset points')
For more explanation see the examples here and the more detailed examples here.
有关更多说明,请参阅此处的示例以及此处的更详细示例。
#2
23
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:
我最初发布问题时不确定这是否可用,但现在可以使用loc参数。以下是一个例子:
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()
#1
34
Just use annotate
and specify axis coordinates. For example, "upper left" would be:
只需使用注释并指定轴坐标。例如,“左上角”将是:
plt.annotate('Something', xy=(0.05, 0.95), xycoords='axes fraction')
You could also get fancier and specify a constant offset in points:
你也可以变得更漂亮,并指定一个点的常量偏移:
plt.annotate('Something', xy=(0, 1), xytext=(12, -12), va='top'
xycoords='axes fraction', textcoords='offset points')
For more explanation see the examples here and the more detailed examples here.
有关更多说明,请参阅此处的示例以及此处的更详细示例。
#2
23
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:
我最初发布问题时不确定这是否可用,但现在可以使用loc参数。以下是一个例子:
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()