2016-07-20 6 views
1

私はmatplotlib.pyplot.subplotsでプロットする一連の関連する関数を持っています。そして、それぞれのサブプロットに対応する関数のズーム部分を含める必要があります。サブプロットに軸を追加する方法は?

私はhereのように説明し始めました。グラフが1つの場合は完全に機能しますが、サブプロットでは機能しません。

私がサブプロットでそれを行うと、その中にすべての関数を持つ単一のグラフしか得られません。ここで私はこれまで何を得るの一例である:私は、各副図に新しい軸とプロットを作成するにはどうすればよい

enter image description here

import numpy as np 
import matplotlib.pyplot as plt 

x = np.arange(-10, 10, 0.01) 
sinx = np.sin(x) 
tanx = np.tan(x) 

fig, ax = plt.subplots(1, 2, sharey='row', figsize=(9, 3)) 

for i, f in enumerate([sinx, cosx]): 
    ax[i].plot(x, f, color='red') 
    ax[i].set_ylim([-2, 2]) 

    axx = plt.axes([.2, .6, .2, .2],) 
    axx.plot(x, f, color='green') 
    axx.set_xlim([0, 5]) 
    axx.set_ylim([0.75, 1.25]) 

plt.show(fig) 

コードの一部は、以下のグラフを与えること?私は、各サブプロットでそれらのほとんどのボックスのいずれかをしたい@jeanrjc

+0

を使用することができます。最初のものでは、第1の関数の一部(sinx)、第2の関数の第2の関数(tanx)の一部が必要です。今、小さな箱は大きな図形に従って配置され、両方の機能は同じ箱にあります。 – Luis

+0

私は結局それを理解するので、私はコメントを削除し、代わりに答えを出しました。 – jrjc

答えて

2

私はよく理解した場合、あなたはinset_axes

import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.axes_grid.inset_locator import inset_axes 


x = np.arange(-10, 10, 0.01) 
sinx = np.sin(x) 
tanx = np.tan(x) 

fig, ax = plt.subplots(1, 2, sharey='row', figsize=(9, 3)) 

for i, f in enumerate([sinx, tanx]): 
    ax[i].plot(x, f, color='red') 
    ax[i].set_ylim([-2, 2]) 

    # create an inset axe in the current axe: 
    inset_ax = inset_axes(ax[i], 
          height="30%", # set height 
          width="30%", # and width 
          loc=10) # center, you can check the different codes in plt.legend? 
    inset_ax.plot(x, f, color='green') 
    inset_ax.set_xlim([0, 5]) 
    inset_ax.set_ylim([0.75, 1.25]) 
plt.show() 

inset_axe

+0

それは本当です!ありがとう! :) – Luis

+0

このエラーに注意してください:https://stackoverflow.com/questions/45103248/importerror-no-module-named-matplotlib-externals – Luis

関連する問題