簡體   English   中英

插入軸的具體位置

[英]specific location for inset axes

我想創建一組軸,以在父組軸的特定位置形成插入。 因此,在inset_axes使用參數loc=1,2,3inset_axes ,如下所示:

inset_axes = inset_axes(parent_axes,
                    width="30%", # width = 30% of parent_bbox
                    height=1., # height : 1 inch
                    loc=3)

但是,我想要接近這一點。 這里這里的答案似乎是比我更復雜的問題的答案。

所以,問題是我可以在上面的代碼中替換一個參數,它允許插入軸在父軸中的自定義位置嗎? 我曾嘗試使用bbox_to_anchor但不理解文檔中的規范或行為。 具體來說,我嘗試過:

 inset_axes = inset_axes(parent_axes,
                        width="30%", # width = 30% of parent_bbox
                        height=1., # height : 1 inch
                        bbox_to_anchor=(0.4,0.1))

嘗試使插圖的左側和底部的錨點分別位於x軸和y軸的40%和10%處。 或者,我試圖把它放在絕對坐標中:

inset_axes = inset_axes(parent_axes,
                            width="30%", # width = 30% of parent_bbox
                            height=1., # height : 1 inch
                            bbox_to_anchor=(-4,-100))

這些都沒有正常工作,並給了我一個我無法解釋的警告。

更一般地說,似乎loc在屬於matplotlib許多函數中都是一個非常標准的參數,那么,這個問題有一個通用的解決方案可以在任何地方使用嗎? 看起來這就是bbox_to_anchor ,但是再一次,我無法弄清楚如何正確使用它。

你采取的方法原則上是正確的。 但是,就像使用bbox_to_anchor 放置圖例 bbox_to_anchor ,該位置被確定為bbox_to_anchorloc之間的相互作用。 上述鏈接答案中的大多數解釋也適用於此。

inset_axes的默認locloc=1 (“右上角”)。 這意味着如果您指定bbox_to_anchor=(0.4,0.1) ,那么它們將是右上角的坐標,而不是左下角的坐標。
因此,您需要指定loc=3以使插入的左下角定位在(0.4,0.1)

但是,如果不以相對單位( "30%" )指定寬度和高度,則將邊界指定為2元組才有意義。 或者換句話說,為了使用相對單位,您需要為bbox_to_anchor使用4元組表示法。

如果在軸單位中指定bbox_to_anchor ,則需要再次使用bbox_transform參數,就像這里解釋的圖例一樣,並將其設置為ax.transAxes

plt.figure(figsize=(6,3))
ax = plt.subplot(221)
ax.set_title("100%, (0.5,1-0.3,.3,.3)")
ax.plot(xdata, ydata)
axins = inset_axes(ax, width="100%", height="100%", loc='upper left',
                   bbox_to_anchor=(0.5,1-0.3,.3,.3), bbox_transform=ax.transAxes)


ax = plt.subplot(222)
ax.set_title("30%, (0.5,0,1,1)")
ax.plot(xdata, ydata)
axins = inset_axes(ax, width="30%", height="30%", loc='upper left',
                   bbox_to_anchor=(0.5,0,1,1), bbox_transform=ax.transAxes)

在此輸入圖像描述

在matplotlib頁面上找到完整的示例: Inset Locator Demo

另一個選擇是使用InsetPosition而不是inset_axes並為現有軸提供一個新位置。 InsetPosition將標准化軸坐標中軸的左下角的x和y坐標以及寬度和高度作為輸入。

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

fig, ax= plt.subplots()

iax = plt.axes([0, 0, 1, 1])
ip = InsetPosition(ax, [0.4, 0.1, 0.3, 0.7]) #posx, posy, width, height
iax.set_axes_locator(ip)

iax.plot([1,2,4])
plt.show()

最后應該提一下,從matplotlib 3.0開始,你可以使用matplotlib.axes.Axes.inset_axes

import matplotlib.pyplot as plt

plt.figure(figsize=(6,3))
ax = plt.subplot(221)
ax.set_title("ax.inset_axes, (0.5,1-0.3,.3,.3)")
ax.plot([0,4], [0,10])
axins = ax.inset_axes((0.5,1-0.3,.3,.3))

plt.show()

結果大致相同,只是mpl_toolkits.axes_grid1.inset_locator.inset_axes允許在軸周圍填充(並默認應用它),而Axes.inset_axes沒有這種填充。

在此輸入圖像描述

使用來自ImportanceOfBeingErnest的答案以及未發布的matplotlib文檔中的幾個建議鏈接(如定位器演示inset_axes文檔) ,我仍然花了一些時間來弄清楚所有參數的表現。 所以,為了清楚起見,我將在此重復我的理解。 我最終使用:

bbox_ll_x = 0.2
bbox_ll_y = 0
bbox_w = 1
bbox_h = 1
eps = 0.01
inset_axes = inset_axes(parent_axes, 
               height="30%", #height of inset axes as frac of bounding box
               width="70%",  #width of inset axes as frac of bounding box
               bbox_to_anchor=(bbox_ll_x,bbox_ll_y,bbox_w-bbox_ll_x,bbox_h), 
               loc='upper left',
               bbox_transform=parent_axes.transAxes)

parent_axes.add_patch(plt.Rectangle((bbox_ll_x, bbox_ll_y+eps),
               bbox_w-eps-bbox_ll_x, 
               bbox_h-eps, 
               ls="--", 
               ec="c", 
               fc="None",
               transform=parent_axes.transAxes))

bbox_ll_x是父軸坐標中邊界框左下角的x位置(這是bbox_transform輸入的含義)

bbox_ll_y是父軸坐標中邊界框左下角的y位置

bbox_w是父軸坐標中邊界框的寬度

bbox_h是父軸坐標中邊界框的高度

eps是一個小數字,用於在繪制矩形邊界框時使矩形從軸下方顯示。

我使用add_patch調用來放置一條青色虛線,表示繪制的邊界框的內邊緣。

對我來說最棘手的部分是意識到heightwidth輸入(當指定為百分比時)相對於邊界框大小。 這就是為什么(如下面的鏈接和答案中所述),如果以百分比形式指定插入軸的大小,則必須bbox_to_anchor參數指定4元組。 如果您將插入軸的大小指定為百分比並且提供bbox_wbbox_hmatplotlib如何獲得插入的絕對大小?

另一件事是loc參數指定在邊界框內錨定插入軸的位置。 據我所知,這是該參數的唯一功能。

暫無
暫無

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

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