2016-12-13 5 views
2

これは少し説明が難しいものです。基本的には、インセットプロットを作成し、mpl_toolkits.axes_grid1.inset_locator.mark_insetの利便性を利用したいが、インセットプロットのデータを親軸のデータとは完全に独立させたい。私が使用したい機能を有するmatplotlibインセットプロット内の異なるデータを持つmark_inset

例コード:だから要約する enter image description here

import numpy as np 

import matplotlib.pyplot as plt 
from mpl_toolkits.axes_grid1.inset_locator import inset_axes 
from mpl_toolkits.axes_grid1.inset_locator import mark_inset 
from mpl_toolkits.axes_grid1.inset_locator import InsetPosition 

data = np.random.normal(size=(2000,2000)) 
plt.imshow(data, origin='lower') 
parent_axes = plt.gca() 

ax2 = inset_axes(parent_axes, 1, 1) 
ax2.plot([900,1100],[900,1100]) 

# I need more control over the position of the inset axes than is given by the inset_axes function 
ip = InsetPosition(parent_axes,[0.7,0.7,0.3,0.3]) 
ax2.set_axes_locator(ip) 

# I want to be able to control where the mark is connected to, independently of the data in the ax2.plot call 
mark_inset(parent_axes, ax2, 2,4) 

# plt.savefig('./inset_example.png') 
plt.show() 

コード例は、次の画像を生成する青色のボックスの位置は全体でax2.plot()への入力データによって制御されます。私は手動で青いボックスを配置し、私がax2にしたいものを入力したいと思います。これは可能ですか?

クイック編集:明確にするために、インセットプロットにデータがリンクされている理由を理解しています。したがって、これを達成するためにmatplotlibに完全に異なる方法がある場合は、自由にその点について返信してください。しかし、大きな画像にかなりの数のインセットが必要なので、私が配置するすべての軸に手作業でボックスや線を配置するのを避けようとしています。

答えて

2

FWIW、私は働くハックを思いついた。 inset_locatorのソースコードで

、私はTransformedBboxを定義するために使用される軸の別のセットを取るmark_insetのバージョンを追加しました:

def mark_inset_hack(parent_axes, inset_axes, hack_axes, loc1, loc2, **kwargs): 
    rect = TransformedBbox(hack_axes.viewLim, parent_axes.transData) 

    pp = BboxPatch(rect, **kwargs) 
    parent_axes.add_patch(pp) 

    p1 = BboxConnector(inset_axes.bbox, rect, loc1=loc1, **kwargs) 
    inset_axes.add_patch(p1) 
    p1.set_clip_on(False) 
    p2 = BboxConnector(inset_axes.bbox, rect, loc1=loc2, **kwargs) 
    inset_axes.add_patch(p2) 
    p2.set_clip_on(False) 

    return pp, p1, p2 

そして私の元のポストコードでIはIはめ込み軸を行いますボックスは、こと私のハッキング機能に渡し、それが見えないようにしたい:

# location of desired axes 
axdesire = inset_axes(parent_axes,1,1) 
axdesire.plot([100,200],[100,200]) 

mark_inset_hack(parent_axes, ax2, axdesire, 2,4) 

axdesire.set_visible(False) 

今、私は私がマーキングてるはめ込みよりデータ単位内の異なる場所でマークされたボックスがあります。

enter image description here

これは確かに完全なハックですが、この時点では手動で線を描くよりもきれいではありませんが、多くのインセットで概念的によりクリーンなものになると思います。

その他のアイデアはまだ歓迎しています。

2

私が正しく理解していれば、ズームインされたインセットのように見えるが、インセットマーカーの位置には接続されていない、任意の位置に任意にスケールされた軸が必要です。

あなたのアプローチに従うと、set_axes_locator(ip)関数を使用して、別の軸をプロットに追加し、真のインセットの同じ場所に配置することができます。この軸はオリジナルのインセットの後に描画されるので、その上に表示されるので、元のプロットの目盛りを隠すだけで完全に消えるようにする必要があります(set_visible(False)はここでは機能しません。インセットとマーカーの位置)。

import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.axes_grid1.inset_locator import inset_axes, mark_inset, InsetPosition 

data = np.random.normal(size=(200,200)) 
plt.imshow(data, origin='lower') 
parent_axes = plt.gca() 

ax2 = inset_axes(parent_axes, 1, 1) 
ax2.plot([60,75],[90,110]) 
# hide the ticks of the linked axes 
ax2.set_xticks([]) 
ax2.set_yticks([]) 

#add a new axes to the plot and plot whatever you like 
ax3 = plt.gcf().add_axes([0,0,1,1]) 
ax3.plot([0,3,4], [2,3,1], marker=ur'$\u266B$' , markersize=30, linestyle="") 
ax3.set_xlim([-1,5]) 
ax3.set_ylim([-1,5]) 


ip = InsetPosition(parent_axes,[0.7,0.7,0.3,0.3]) 
ax2.set_axes_locator(ip) 
# set the new axes (ax3) to the position of the linked axes 
ax3.set_axes_locator(ip) 
# I want to be able to control where the mark is connected to, independently of the data in the ax2.plot call 
mark_inset(parent_axes, ax2, 2,4) 

plt.show() 

enter image description here

+0

あなたのハックは、インセット内のデータを台無しにしながら、私は、青い箱の位置が...だけでなく、私のハックを台無しに、このようなものを考えられていました。複数のオプションがあると便利で、mplのソースコードをハックする必要はありません。 –

関連する問題