这是一个有点棘手的解释。基本上,我想制作一个插入图,然后利用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()