2015-12-20 181 views
0

如何定位文字藝術家相對於其他文字藝術家或傳奇藝術家等。假設我想在圖例下放置一些任意文本,但圖例的條目數量可變。因此圖例在y維上跨越了未知的距離。無論傳奇有多大或多小,我都希望將我的文本放在它的下面。如何將文字藝術家定位於傳奇藝術家?

import matplotlib as mpl 
import matplotlib.pyplot as plt 
import matplotlib.lines as lines 

plt.figure(figsize=(11., 8.5)) 
plt.gcf().add_axes([0.05,0.05,0.6,0.6]) 
bar=lines.Line2D([],[], color="0.1", linewidth=2,ls=":", marker="1") 
plt.gca().legend([bar],["foo"],bbox_to_anchor=[1.,1.], loc="upper left") 
s=r"$\alpha$" 
s+=r"$\alpha_i \beta_j \gamma^k$" 
plt.text(1.02,0.8,s,transform=plt.gca().transData, wrap =True, fontsize ="xx-small") 
plt.show() 

在此腳本中,我想將文字藝術家錨定到圖例藝術家的底部。

回答

0

我非常確定,如果你會搜索,你將能夠找到幾個如何做到這一點的例子。

下面是做到這一點的一種方法:

fig = plt.figure(figsize=(11., 8.5)) 
ax = fig.add_axes([0.05,0.05,0.6,0.6]) 
bar=lines.Line2D([],[], color="0.1", linewidth=2,ls=":", marker="1") 
leg = ax.legend([bar],["foo"],bbox_to_anchor=[1.,1.], loc="upper left") 
s=r"$\alpha$" 
s+=r"$\alpha_i \beta_j \gamma^k$" 

# You need to draw the artists before you can get its coordinates: 
plt.draw() 

# Now get coordinates of the legend 
p = leg.get_window_extent().inverse_transformed(ax.transAxes) 

# Set offset of the text in 'axes fraction' units 
# (if it is 0 text will be placed within the legend box, set 'zorder=9' as parameter to ax.annotate to see it) 
offset = 0.03 

# Now place the text on the plot 
ax.annotate('Annotation', (p.p0[0], p.p0[1] - offset), xycoords='axes fraction') 

plt.show() 

enter image description here

+0

這就是超讚! – EricVonB