gpt4 book ai didi

python - 在插图图中具有不同数据的 matplotlib mark_inset

转载 作者:行者123 更新时间:2023-11-28 21:44:10 27 4
gpt4 key购买 nike

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

示例代码生成以下图像: enter image description here

综上所述:蓝框的位置完全由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()

enter image description here

关于python - 在插图图中具有不同数据的 matplotlib mark_inset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41130989/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com