2017-05-26 6 views
0

私はmatplotlib.image.AxesImageオブジェクトを返す関数を持っています。これらのオブジェクトの2つを同じ図に並べてプロットしたいと思います。側(see current plots output here)に並べてJupiterノートブックで2つのmatplotlib.image.AxesImageオブジェクトを並べて表示

fig, (ax1, ax2) = plt.subplots(ncols=2) 
ax1 = function_that_returns_AxesImage(some_arguments) # tried to reassign ax1 
ax2 = function_that_returns_AxesImage(other_arguments) # tried to reassign ax2 

は、しかし、それはちょうど2つの空のサブプロットして図を生成し、私が欲しい二つのグラフがプロットされている列ではなく、私はこのような何かを試してみました。 私の問題は、function_that_returns_axesを使用する必要があり、返されたプロットをサブプロットに配置する方法がわかりません。あるいは、私がJupyterに2つの図を並べて表示することができれば、それもうまくいくかもしれません。

+0

私たちはfunction_that_returns_axes' 'に関するより多くの情報が必要だと思います。実際にはコードの一部を考慮しているようですが、それ以外の場合は同じ既存の図にプロットされません。この機能はどこからもたらされますか?あなた自身で書きましたか?あなたはそれにリンクを付けることができますか? – ImportanceOfBeingErnest

+0

@ImportanceOfBeingErnest確かに、galpyライブラリ[https://github.com/jobovy/galpy.git](https://github.com/jobovy/galpy.git)で作業しています。特定の関数は、galpy/galpy/potential_src/Potential.pyにあるプロット関数です。ご協力いただきありがとうございます! –

+0

私の答えはあなたのために働くのですか?もしそうなら、私はガルピーコードを深く見る必要はありません。 – ImportanceOfBeingErnest

答えて

0

ソリューションは、もちろん正確function_that_returns_axesがに何をするかに依存します数字。しかし、それは見えるように、既存の図形を考慮に入れ、それがプロットした軸を返します。

この軸を受け取り、その位置がユーザ定義のグリッド上にあるように変更することができます。グリッドはmatplotlib.gridspecによって作成されます。彼らは、最初function_that_returns_axesによって互いの上に作成されているが、両方の軸が互いに隣接している見ることができるように

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 

fig = plt.figure() 

def function_that_returns_axes(l="A"): 
    ax = fig.add_subplot(111, label=l) 
    ax.plot(np.random.rand(5)) 
    return ax 

ax1 = function_that_returns_axes("A") 
ax2 = function_that_returns_axes("B") 

gs = gridspec.GridSpec(1,2) 
ax1.set_position(gs[0].get_position(fig)) 
ax1.set_subplotspec(gs[0]) 

ax2.set_position(gs[1].get_position(fig)) 
ax2.set_subplotspec(gs[1]) 

plt.show() 

enter image description here

関数は軸を返さない場合は、しかし、画像次のように、解決策は次のようになります。

def function_that_returns_image(l="A"): 
    ax = fig.add_subplot(111, label=l) 
    im = ax.imshow(np.random.rand(5,5)) 
    return im 

im1 = function_that_returns_image("A") 
im2 = function_that_returns_image("B") 

ax1 = im1.axes 
ax2 = im2.axes 

# the rest being the same as above... 

enter image description here

0

あなたはfunction_that_returns_axesax1, ax2を渡すことができない場合は、次のコードを使用することができます。詳細については

def align_figures(): 
    import matplotlib 
    from matplotlib._pylab_helpers import Gcf 
    from IPython.display import display_html 
    import base64 
    from ipykernel.pylab.backend_inline import show 

    images = [] 
    for figure_manager in Gcf.get_all_fig_managers(): 
     fig = figure_manager.canvas.figure 
     png = get_ipython().display_formatter.format(fig)[0]['image/png'] 
     src = base64.encodebytes(png).decode() 
     images.append('<img style="margin:0" align="left" src="data:image/png;base64,{}"/>'.format(src)) 

    html = "<div>{}</div>".format("".join(images)) 
    show._draw_called = False 
    matplotlib.pyplot.close('all') 
    display_html(html, raw=True) 

Jupyter Notebook: Output image in previous line

関連する問題