2017-02-16 9 views
0

私はmatplotlibでマルチプロットを生成し、私のサブプロットの各コーナーに画像を埋め込むしたいと思います。matplotlibの:いくつかのサブプロットで埋め込み画像()

Iはfollowing example of the matplotlib documentation(以下提供されたコード)を用いて、(単一)のプロット図で画像を埋め込むことができました。

私は今、サブプロットのシリーズのそれぞれの隅に画像を埋め込むしようとしています。前の例が依存していた図のadd_axes()に似た関数を見つけることができないようです。

希望のレイアウトをどのように達成できますか?

import pylab as plt 
from numpy import linspace 
from matplotlib.cbook import get_sample_data 
from scipy.misc import imread 

xs = linspace(0, 1, 100) 

def single_plot(): 
    fig, ax = plt.subplots() 
    ax.plot(xs, xs**2) 

    fn = get_sample_data("grace_hopper.png", asfileobj=False) 
    image_axis = fig.add_axes([0.65, 0.70, 0.3, 0.2], anchor='NE', zorder=10) 
    image_axis.imshow(imread(fn)) 

    plt.show() 
    plt.clf() 

def multi_plot(): 
    fig, axes = plt.subplots(4) 
    for axis in axes: 
     axis.plot(xs, xs**2) 
     # How to draw the same image as before in the corner of each subplot ? 
    plt.show() 

if __name__ == '__main__': 
    single_plot() 
    multi_plot() 

答えて

2

同じ方法を使用して、複数のサブプロットに画像を重ねることができます。図形全体に対してオーバーレイしたい画像ごとに、軸の位置を定義する必要があります。ここでは、コードとMatplotlibのドキュメントを使用した簡単な例を示します。

def multi_plot(): 
    fig, axes = plt.subplots(4, 1, figsize=(8, 10)) 
    fn = get_sample_data("grace_hopper.png", asfileobj=False) 
    image = plt.imread(fn) 
    x = 0 
    for axis in axes: 
     axis.plot(xs, xs**2) 

     # [left, bottom, width, height] 
     image_axis = fig.add_axes([0.125, 0.25 + x, 0.15, 0.65], 
           zorder=10, anchor="N") 
     image_axis.imshow(image) 
     image_axis.axis('off') 
     x -= 0.209 

    plt.show() 

図の下部を基準にして新しい軸の位置を徐々に減らすことを選択しました。追加する各画像オーバーレイの正確な位置を指定することもできます。

上記のコードは次のようになりますプロットを生成する:ベルトラン・キャロン@ Image Overlay Subplots

+0

この答えはあなたの質問を解決した場合は、[ソリューションとして、それを受け入れる]ご検討ください(http://stackoverflow.com/help /誰かの回答)。 – Brian

関連する問題