2013-10-16 4 views
5

2つのプロットのx軸を、imshowプロットの場合は、調整したいと考えています。matshotlibで2つのプロットを縦に並べると、1つはimshowプロットですか?

私はそれは次のようにgridspecを使用しようとしました:

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

v1 = np.random.rand(50,150) 
v2 = np.random.rand(150) 

fig = plt.figure() 

gs = grd.GridSpec(2,1,height_ratios=[1,10],wspace=0) 


ax = plt.subplot(gs[1]) 
p = ax.imshow(v1,interpolation='nearest') 
cb = plt.colorbar(p,shrink=0.5) 
plt.xlabel('Day') 
plt.ylabel('Depth') 
cb.set_label('RWU') 
plt.xlim(1,140) 

#Plot 2 
ax2 = plt.subplot(gs[0]) 
ax2.spines['right'].set_visible(False) 
ax2.spines['top'].set_visible(False) 
ax2.xaxis.set_ticks_position('bottom') 
ax2.yaxis.set_ticks_position('left') 
x=np.arange(1,151,1) 
ax2.plot(x,v2,'k',lw=0.5) 
plt.xlim(1,140) 
plt.ylim(0,1.1) 
# 
plt.savefig("ex.pdf", bbox_inches='tight') 

私もお互いにできるだけ近いプロットや他の1つの1/10高さが欲しいです。私がカラーバーを外したら、彼らは整列しているように見えますが、まだ私はそれらをお互いに接近させることはできません。私もカラーバーが欲しい。

答えて

12

Figureの縦横比が軸と異なるため、イメージがスペースを埋めることはありません。 1つの方法は、画像のアスペクト比を変更することです。 2つのグリッドを使用し、カラーバーをそれ自身の軸に置くことによって、イメージと折れ線グラフを整列させたままにすることができます。

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

v1 = np.random.rand(50,150) 
v2 = np.random.rand(150) 

fig = plt.figure() 

# create a 2 X 2 grid 
gs = grd.GridSpec(2, 2, height_ratios=[1,10], width_ratios=[6,1], wspace=0.1) 

# image plot 
ax = plt.subplot(gs[2]) 
p = ax.imshow(v1,interpolation='nearest',aspect='auto') # set the aspect ratio to auto to fill the space. 
plt.xlabel('Day') 
plt.ylabel('Depth') 
plt.xlim(1,140) 

# color bar in it's own axis 
colorAx = plt.subplot(gs[3]) 
cb = plt.colorbar(p, cax = colorAx) 
cb.set_label('RWU') 

# line plot 
ax2 = plt.subplot(gs[0]) 

ax2.spines['right'].set_visible(False) 
ax2.spines['top'].set_visible(False) 
ax2.xaxis.set_ticks_position('bottom') 
ax2.yaxis.set_ticks_position('left') 
ax2.set_yticks([0,1]) 
x=np.arange(1,151,1) 
ax2.plot(x,v2,'k',lw=0.5) 
plt.xlim(1,140) 
plt.ylim(0,1.1) 

plt.show() 

aligned image and line plot withe color bar

+0

@Molly、ありがとうございます。今私はこれを管理し、プロットの他のパラメータを変更する方法を理解することができました。 –

関連する問題