画像の一部を拡大してmatplotlibの同じプロットに挿入する方法 質問する

画像の一部を拡大してmatplotlibの同じプロットに挿入する方法 質問する

データ/画像の一部を拡大し、同じ図の中にプロットしたいと思います。次の図のようになります。

拡大図

同じプロット内にズーム画像の一部を挿入することは可能ですか。サブプロットで別の図を描くことは可能だと思いますが、2 つの異なる図が描画されます。また、長方形/円を挿入するためのパッチを追加することも読みましたが、画像の一部を図に挿入することが有用かどうかはわかりません。基本的には、テキスト ファイルからデータを読み込み、以下に示す簡単なプロット コマンドを使用してプロットします。

matplotlib画像ギャラリーから関連する例を1つ見つけましたここしかし、どのように機能するかはよく分かりません。ご協力いただければ幸いです。

from numpy import *
import os
import matplotlib.pyplot as plt
data = loadtxt(os.getcwd()+txtfl[0], skiprows=1)
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.semilogx(data[:,1],data[:,2])
plt.show()

ベストアンサー1

最も簡単な方法は、「zoomed_inset_axes」と「mark_inset」を組み合わせることです。その説明と関連する例については、以下を参照してください。AxesGrid ツールキットの概要

ここに画像の説明を入力してください

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()

おすすめ記事