简体   繁体   English

将mark_inset与其他范围图一起使用

[英]Use mark_inset with different range plot

Say I want to inset a plot to a figure, but the inset plot has different axis range than the one I am marking the inset to. 假设我要在图形上插入图,但是插入图的轴范围与我要标记插入图的轴范围不同。 For example: 例如:

fig, ax = plt.subplots()
axins = inset_axes(ax, 1,1 , loc=2, bbox_to_anchor=(0.35,0.85),bbox_transform=ax.figure.transFigure)

x = np.linspace(0, 3, 100)
y = x**2
ax.plot(x, y)
axins.plot(x, x**3)

x1, x2, y1, y2 = 2.,3, 6, 8 # specify the limits
axins.set_xlim(x1, x2) # apply the x-limits
axins.set_ylim(y1, y2) # apply the y-limits

plt.xticks(visible=False)
plt.yticks(visible=False)

mark_inset(ax, axins, loc1=4, loc2=1)#, fc="none")#, ec="0.5")

The result is an empty inset plot: 结果是一个空的插图:

在此处输入图片说明

But this is obvious, since I set the limits of x and y to ranges where x**3 does not pass. 但这很明显,因为我将xy的限制设置为x**3不通过的范围。 What I want to see is, for example, a plot of x**3 for 0 to 1 in the inset plot, while the mark_inset will still 'zoom' to the region boxed above, which is of different range. 我想看到的是,例如,在插入图中以01绘制x**3的图,而mark_inset仍将“缩放”到上面框出的区域,该区域具有不同的范围。

How can I do this? 我怎样才能做到这一点?

In that case you cannot use mark_inset directly, because that is exactly what this function does: synchronizing the marker with the axes limits of the inset. 在那种情况下,您不能直接使用mark_inset ,因为这正是该功能的作用:将标记与插图的轴限制同步。

Using a rectangle 使用矩形

Instead you may position some rectangle whereever you want it to be and use ConnectionPatch es to draw some lines in between the inset and the rectangle. 取而代之的是,您可以将矩形放置在所需的位置,并使用ConnectionPatch es在插图和矩形之间绘制一些线。

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axes_grid1.inset_locator as il
import matplotlib.patches as mpatches

fig, ax = plt.subplots()

axins = il.inset_axes(ax, 1,1 , loc=2, bbox_to_anchor=(0.35,0.85),bbox_transform=ax.figure.transFigure)

x = np.linspace(0, 3, 100)
y = x**2
ax.plot(x, y)
axins.plot(x, x**3)

x1, x2, y1, y2 = 2.,3, 6, 8 # specify the limits
rect = mpatches.Rectangle((x1,y1), width=x2-x1, height=y2-y1, facecolor="None", edgecolor="k", linewidth=0.8)
fig.canvas.draw()
p1 = mpatches.ConnectionPatch(xyA=(1,0), xyB=(x2,y1), coordsA="axes fraction", coordsB="data",  axesA=axins, axesB=ax)
p2 = mpatches.ConnectionPatch(xyA=(1,1), xyB=(x2,y2), coordsA="axes fraction", coordsB="data",  axesA=axins, axesB=ax)

ax.add_patch(rect)
ax.add_patch(p1)
ax.add_patch(p2)

plt.show()

Use dummy axes 使用虚拟轴

You may also simply add an additional inset, just for the purpose of using mark_inset with it. 您也可以简单地添加一个附加插图,仅用于与它一起使用mark_inset的目的。

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axes_grid1.inset_locator as il

fig, ax = plt.subplots()
axins_dummy = il.inset_axes(ax, 1,1 , loc=2, bbox_to_anchor=(0.35,0.85),bbox_transform=ax.figure.transFigure)
axins = il.inset_axes(ax, 1,1 , loc=2, bbox_to_anchor=(0.35,0.85),bbox_transform=ax.figure.transFigure)

x = np.linspace(0, 3, 100)
y = x**2
ax.plot(x, y)
axins.plot(x, x**3)

x1, x2, y1, y2 = 2.,3, 6, 8 # specify the limits
axins_dummy .set_xlim(x1, x2) # apply the x-limits
axins_dummy .set_ylim(y1, y2) # apply the y-limits

axins_dummy.tick_params(left=False, bottom=False, labelleft=False, labelbottom=False )

il.mark_inset(ax,axins_dummy , loc1=4, loc2=1)#, fc="none")#, ec="0.5")

plt.show()

In both cases, the resulting plot would look like 在这两种情况下,生成的图看起来像

在此处输入图片说明

Maybe it's worth noting that the resulting graph is of course incorrect. 也许值得注意的是,结果图当然是不正确的。 Any reader would assume that the inset shows part of the curve, which is not the case. 任何读者都将假设插图显示了曲线的一部分,事实并非如此。 Hence make sure not to use such graph in a publication or report. 因此,请确保不要在出版物或报告中使用此类图表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM