簡體   English   中英

matplotlib mark_inset與插圖中的不同數據

[英]matplotlib mark_inset with different data in inset plot

這是一個有點棘手的解釋。 基本上,我想制作一個插入圖,然后利用mpl_toolkits.axes_grid1.inset_locator.mark_inset的便利性,但是我希望插入圖中的數據完全獨立於父軸中的數據。

具有我要使用的功能的示例代碼:

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中有一種完全不同的方式來完成此任務,請隨時回復。 但是,我試圖避免將框和線手動放置到要放置的所有軸上,因為我需要在大圖像中插入很多插圖。

如果我理解正確,則您希望在給定位置上任意縮放的軸看起來像縮放的插圖,但與插圖標記的位置沒有關系。

按照您的方法,您可以簡單地使用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()

在此處輸入圖片說明

FWIW,我想出了一個可行的方法。

在inset_locator的源代碼中,我添加了一個mark_inset版本,該版本采用了另一組用於定義TransformedBbox的軸:

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

然后在我的原始帖子代碼中,創建一個插入軸,將框放置在其中,將其傳遞給被黑的函數,並使它不可見:

# 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)

現在,我在數據單元中與我標記的插圖不同的位置有一個標記框:

在此處輸入圖片說明

當然,這絕對是一個hack,目前還不確定它是否比手動繪制線條更干凈,但是我認為對於很多插圖而言,這將使概念更清晰。

仍然歡迎其他想法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM