2017-02-17 8 views
1

matplotlibを使用して、凡例の下に図に関するいくつかの注釈を含むテキストボックスを配置したいと思います。私の伝説は右の軸の外側です。私の計画は、図の基準の枠内で凡例の位置を見つけて、図のtextの方法を使ってメモを配置することでした。しかし、私はの凡例の座標を取得する方法はできません。どんな提案や代替計画も大変ありがとうございます。matplotlibの凡例の下にテキストボックスを置く方法は?

答えて

2

レンダリングされた凡例の位置を読み取ることができるのは明らかです。座標はピクセル単位になります。次に、凡例の座標と図の寸法を使用して凡例のすぐ下にあるfig.add_axesを使用して新しい軸を作成することができます。この図を生成

from matplotlib.pyplot import subplots 
fig,ax = subplots() 
fig.subplots_adjust(right=0.75) 
ax.plot([0,1],'.-',label="line1") 
ax.plot([0.1,1.1],'.-',label="line2") 
leg = ax.legend(bbox_to_anchor=(1.05, 1),loc=2, borderaxespad=0) 

fig.canvas.draw() # this draws the figure 
        # which allows reading final coordinates in pixels 
leg_pxls = leg.get_window_extent() 
ax_pxls = ax.get_window_extent() 
fig_pxls = fig.get_window_extent() 

# Converting back to figure normalized coordinates to create new axis: 
pad = 0.025 
ax2 = fig.add_axes([leg_pxls.x0/fig_pxls.width, 
        ax_pxls.y0/fig_pxls.height, 
        leg_pxls.width/fig_pxls.width, 
        (leg_pxls.y0-ax_pxls.y0)/fig_pxls.height-pad]) 

# eliminating all the tick marks: 
ax2.tick_params(axis='both', left='off', top='off', right='off', 
       bottom='off', labelleft='off', labeltop='off', 
       labelright='off', labelbottom='off') 

# adding some text: 
ax2.text(0.1,0.1,"some text\nabout the\nlines") 

::必要でない場合、フレームは簡単に切ることができ

enter image description here

ここでは一例です。

関連する問題