2016-05-12 7 views
3

pythonとmatplotlibを使って画像をプロットするには? 2Dヒートマップをプロットする方法はわかっていますが、バーをヒートマップの上にプロットしたり、カラーバーとヒートマップの間のバーをプロットしたりすることで多くのことを挫折させました。 これらの2つの棒を画像に追加する方法と、x軸またはy軸の数字をどのグループに表示するか?matplotlibでヒートマップをプロットする方法は?

ありがとうございました。

enter image description here

答えて

5

体系的かつ直接的なアプローチであるが、もう少し面倒開始時には、matplotlib.gridspec.GridSpecを使用することです。

import matplotlib.pyplot as plt 
from matplotlib.gridspec import GridSpec 

fig = plt.figure() 
gs = GridSpec(2, 3, width_ratios=[10, 1, 1], height_ratios=[1, 10]) 

これは私達に左下軸が10×10になると、他の軸は相対的なサイズで10x1では又は1×10のいずれかになります2行3列のグリッドを与える:

最初にグリッドを設定します。これらの比率は好みに合わせて調整することができます。上の中央/右の軸は空になることに注意してください。

big_ax = fig.add_subplot(gs[1,0]) # bottom left 
top_ax = fig.add_subplot(gs[0,0]) # top left 
right_ax = fig.add_subplot(gs[1,1]) # bottom center 
cbar_ax = fig.add_subplot(gs[1,2]) # bottom right 

私は上と右の画像は、Google経由で見つかった一般的なゲノムの画像を使用します:enter image description here

とランダムヒートマップが生成されます。 imshow(aspect='auto')を使用して、イメージオブジェクトとヒートマップがそれぞれの軸の全領域を占めるようにします(そうしないと、gridspecによって設定された高さ/幅の比率がオーバーライドされます)。

im = plt.imread('/path/to/image.png') 
# Plot your heatmap on big_ax and colorbar on cbar_ax 
heatmap = big_ax.imshow(np.random.rand(10, 10), aspect='auto', origin='lower') 
cbar = fig.colorbar(heatmap, cax=cbar_ax) 

# Show your images on top_ax and right_ax 
top_ax.imshow(im, aspect='auto') 

# need to rotate my image. 
# you may not have to if you have two different images 
from scipy import ndimage 
right_ax.imshow(ndimage.rotate(im, 90), aspect='auto') 

# Clean up the image axes (remove ticks, etc.) 
right_ax.set_axis_off() 
top_ax.set_axis_off() 

# remove spacing between axes 
fig.subplots_adjust(wspace=0.05, hspace=0.05) 

enter image description here

それは(特にデフォルトのジェットカラーマップ付き)スーパーグラマラスではないのですが、あなたは簡単に数字をあなたのOPを再現するためにこれを使用することができます。



編集:あなたは上部と右側にそのゲノムのようなプロットを生成したいのであれば、あなたはトップバーのために、このような何かを試みることができる:

については
from matplotlib.patches import Rectangle 
from matplotlib.collections import PatchCollection 

# draw the black line 
top_ax.axhline(0, color='k', zorder=-1) 

# box x-coords and text labels 
boxes = zip(np.arange(0.1, 1, 0.2), np.arange(0.2, 1, 0.2)) 
box_text = ('A1', 'B1', 'B2', 'A2') 
# color indicators for boxes 
colors = (0, 1, 1, 0) 
# construct Rects 
patches = [Rectangle(xy=(x0, -1), width=(x1-x0), height=2) for x0,x1 in boxes] 
p = PatchCollection(patches, cmap='jet') 
# this maps the colors in [0,1] to the cmap above 
p.set_array(np.array(colors)) 
top_ax.add_collection(p) 

# add text 
[top_ax.text((x0+x1)/2., 1.2, text, ha='center') 
    for (x0,x1), text in zip(boxes, box_text)] 

# adjust ylims 
top_ax.set_ylim(-2, 2) 

あなたは同じことをすることができますが、axvlineを使って、y座標のx座標を入れ替えてください。

right_ax.axvline(0, color='k', zorder=-1) 

patches = [Rectangle(xy=(-1, y0), width=2, height=(y1-y0)) for y0, y1 in boxes] 
p = PatchCollection(patches, cmap='jet') 
p.set_array(np.array(colors)) 
right_ax.add_collection(p) 

[right_ax.text(1.2, (y0+y1)/2., text, va='center') 
    for (y0, y1), text in zip(boxes, box_text)] 
right_ax.set_xlim(-2,2) 

これらの変更は次のようになる:

enter image description here

+0

ご回答をありがとう非常に。しかし、あなたの場合、バーは画像から読み込まれるだけなので、サイズを拡大してX軸の値と正確に一致させることは難しいです。とにかく私たち自身でバーを描くことができますか?ありがとう! –

+0

@ZhouHongyu編集をチェックしてください。 – wflynny

+0

編集に感謝します!私は今この方法を試してみよう! –

関連する問題