2016-08-25 3 views
3

科学的な出版物で部屋が不足しているため、軸の内側にカラーバーを配置したいと思います。 inset_axesはサブ軸を作成するのに適しているようですが、 'loc = 0,1,2,3,4'以外の他の座標で配置するのは簡単ではありません。結果として、カラーマップのラベルは図の外にあります。私は同様のツールを使用して、ax.annotate()内の(x、y)座標を見つけることができます。matplotlibでinset_axesの位置を設定する方法

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

plt.close('all') 

##### build some fake data to map ########## 
x = np.linspace(-0.7,0.7,num = 50) 
y = np.linspace(0,1,num = 100) 
ext = (x[0],x[-1],y[-1],y[0]) 
xx,yy = np.meshgrid(x,y,indexing = 'ij') 
U = np.cos(xx**2 + yy**2) 

#### build figures and axes ########### 
fig, ax = plt.subplots(1,1) 
fig.set_size_inches(4, 3) 
#### plot ############ 
im = ax.imshow(U,interpolation = 'nearest', extent = ext, aspect = 'equal',vmin=0,vmax=np.amax(U)) 

#### insert colorbar ###### 

cax = inset_axes(ax,width = '5%',height = '17%',loc = 1) 
cbar = plt.colorbar(im, ax = ax,cax = cax ,format = '%.1f' ,ticks = [0,np.amax(U)]) 

cax = inset_axes(ax,width = '5%',height = '17%',loc = 3) 
cbar = plt.colorbar(im, ax = ax,cax = cax,format = '%.1f' ,ticks = [0,np.amax(U)]) 

### stuff #### 

fig.tight_layout() 
plt.show() 

enter image description here

よろしく、inset_axesを使用していません

答えて

0

一つの可能​​な解決策は、単にmatplotlibののcolorbarを使用しますが、それは数字の中に現れるようにすることです。

x = np.linspace(-0.7,0.7,num = 50) 
y = np.linspace(0,1,num = 100) 
ext = (x[0],x[-1],y[-1],y[0]) 
xx,yy = np.meshgrid(x,y,indexing = 'ij') 
U = np.cos(xx**2 + yy**2) 

#### build figures and axes ########### 
fig1, ax = plt.subplots(1,1) 
fig1.set_size_inches(4, 3) 
#### plot ############ 
plt.imshow(U,interpolation = 'nearest', extent = ext, aspect = 'equal',vmin=0,vmax=np.amax(U)) 

#### insert colorbar ###### 

cax = fig1.add_axes([0.12, 0.52, 0.86, 0.3]) #change the position and size of the colorbar here 
cax.get_xaxis().set_visible(False) 
cax.get_yaxis().set_visible(False) 
cax.set_frame_on(False) 
cbar = plt.colorbar() 
cbar.set_ticks([0,np.max(U)]) #set the colorbar ticks 

### stuff #### 

plt.show() 

これは、次のグラフが得られる:

enter image description here

+1

[OK]をおかげで、それは、CBAR = plt.colorbar(IM、CAX = CAX、AX = AX) ''この場合に動作しますが私の軸は、実際には単一の図のサブプロットです。多分私は私の例にそれを含めるべきでした。カラーバーの位置は、サブアクセスに対して相対的にではなく、図形に対して相対的に調整するのが便利ではありません。どんなトリックですか? "ax.get_position()"やax.add_subaxes()のように? –

関連する問題