2016-03-07 256 views
2

我想用matplotlib繪製一個插圖縮放的情節。我發現下面的例子中,這將產生一個情節與插圖:Matplotlib,控制mark_inset()屬性(kwargs)

import matplotlib.pyplot as plt 

from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes 
from mpl_toolkits.axes_grid1.inset_locator import mark_inset 

import numpy as np 


def get_demo_image(): 
    from matplotlib.cbook import get_sample_data 
    import numpy as np 
    f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) 
    z = np.load(f) 
    # z is a numpy array of 15x15 
    return z, (-3, 4, -4, 3) 

fig, ax = plt.subplots(figsize=[5, 4]) 

# prepare the demo image 
Z, extent = get_demo_image() 
Z2 = np.zeros([150, 150], dtype="d") 
ny, nx = Z.shape 
Z2[30:30 + ny, 30:30 + nx] = Z 

# extent = [-3, 4, -4, 3] 
ax.imshow(Z2, extent=extent, interpolation="nearest", 
      origin="lower") 

axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6 
axins.imshow(Z2, extent=extent, interpolation="nearest", 
      origin="lower") 

# sub region of the original image 
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9 
axins.set_xlim(x1, x2) 
axins.set_ylim(y1, y2) 

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

# draw a bbox of the region of the inset axes in the parent axes and 
# connecting lines between the bbox and the inset axes area 
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") 

plt.draw() 
plt.show() 

不過,我無法找到正確的參數傳遞mark_inset()功能,以使拳擊線較粗的線寬,或顏色線紅色。

我找不到什麼論點fcec代表,通過搜索文檔。

回答

3

zoomed_inset_axes後添加以下應該做你需要的東西:

axins = zoomed_inset_axes(ax, 6, loc=1) 

for axis in ['top','bottom','left','right']: 
    axins.spines[axis].set_linewidth(3) 
    axins.spines[axis].set_color('r') 

要改變插入線改變爲紅色,如下所示:

mark_inset(ax, axins, loc1=2, loc2=4, fc="none", lw=2, ec='r') 

給你以下類型的輸出:

Matplot lib screenshot

The Mat情節LIB定義fcecmark_inset如下:

  • facecolorfc - MPL顏色規格,或無違約,或無顏色「無」 。
  • edgecolorec-mpl顏色規範,或 默認爲None,或者沒有顏色爲none。
+0

謝謝,如果我想嵌入行是紅色會發生什麼?另一方面,你如何發現zoomed_inset_axes對象有一個名爲spine的屬性?在文檔中導航時我有點迷路。 – Mathusalem

+0

您需要將'mark_inset'函數中的'ec'更改爲一種顏色,例如''r'' –

+0

啊,好的謝謝! – Mathusalem