2012-12-24 34 views
3

我有一个imshow阴谋,显示云和一个叠加颤抖阴谋,显示云运动向量。此图现在以像素显示,但我想以公里显示,即云场景的大小。我可以改变imshow的范围,但是颤抖情节不再适合。matplotlib在颤抖阴谋的变化范围

你有什么建议如何做到这一点?任何帮助,将不胜感激!

圣诞

这里是我的代码:

# size I want to be shown in the plot (in kilometer) 
size = 9.750 

# -> extent[0, size, 0, size] 


# arrays used in plot (pixel size) 

im_current = np.array((275,275)) 
xdis_mean = np.array((275,275)) 
ydis_mean = np.array((275,275)) 


# settings for the quiver plot 

sliceNr=20 # every x pixel will be shown 
sy,sx =np.shape(im_current) 
x=np.arange(sx)[::sliceNr] 
y=np.arange(sy)[::sliceNr] 


# colormap for the quiver plot 
M = sqrt(pow(xdis_mean[::sliceNr,::sliceNr], 2) + pow(ydis_mean[::sliceNr,::sliceNr], 2)) 



fig=plt.figure() 

ax=fig.add_subplot(111) 
cax=ax.imshow(im_current,origin='lower', cmap=cmap,vmin=0,vmax=1,norm=norm) 

setp(plt.Axes.get_xticklabels(plt.gca()), fontsize=10) 
setp(plt.Axes.get_yticklabels(plt.gca()), fontsize=10) 


title('image at t=0 \n with mean displacement vector field') 

xlabel('area size [pixel]',fontsize=9) 
ylabel('area size [pixel]',fontsize=9) 

# get axes from subplot to adjust colorbar to these axes 
divider = make_axes_locatable(plt.gca()) 
cax1 = divider.append_axes("right", "5%", pad="4%") 

cbar1=plt.colorbar(cax,cax=cax1,cmap=cmap,boundaries=bounds,ticks=[0,1],use_gridspec=True) 
cbar1.ax.set_yticklabels(['0','1'],fontsize=10) 


v=ax.quiver(x,y,xdis_mean[::sliceNr,::sliceNr],ydis_mean[::sliceNr,::sliceNr],M, units='xy',angles='xy',scale=1,scale_units='xy',cmap='autumn') 

cax2 = divider.append_axes("bottom", "5%", pad="9%") 
cbar2=plt.colorbar(v,cax=cax2,orientation='horizontal',use_gridspec=True) 
for t in cbar2.ax.get_xticklabels(): 
    t.set_fontsize(10) 

plt.tight_layout() 

show() 

来说明吧,这里的数字: enter image description here

回答

2

有到去这样做双向的:您可以重新 - 放大颤抖的(x,y)数据,或者设置标签格式器。

方案A是这样的:

x,y = x*km_per_pixel + km_offset_x, y*km_per_pixel + km_offset_y 
im = ax.imshow(...,exent=lims_in_km) 
q = ax.quiver(x,y,...) 

B选项是这样的:

q = ax.quiver(..) 
im = ax.imshow(...) # exactly like you had before 
ax.get_xaxis().set_major_formatter(
    matplotlib.ticker.FuncFormatter(
     lambda x,i: '%.2f' % (x * km_per_pixel + km_offset_x))) 
ax.get_yaxis().set_major_formatter(
    matplotlib.ticker.FuncFormatter(
     lambda x,i: '%.2f' % (x * km_per_pixel + km_ofset_y))) 

您应该调整格式化字符串是你什么都喜欢。如果您想要更多地控制,其中的刻度将显示在Locator s之内。 (所有这些课程的文档都在here

+0

非常感谢!我选择了选项A,没有使用“+ km_offset_x”,它就起作用了!其实很简单...不要为什么我有这样的问题找到答案。所以,祝你新年快乐! –

+0

我现在选择了B选项,因为那时我可以在ax.quiver中使用units ='xy',这使得它看起来比单元='dots'好得多,我使用选项A.所以感谢这两个选项! –

+0

很高兴这很有帮助 – tacaswell