2017-08-01 162 views
0

我有一个主要问题,我似乎无法找到任何解决方案。 基本上,我绘制了2D字段与来自matplotlib工具的pcolormesh。python matplotlib当X和Y是矩阵时X勾选位置

现在,如果我想绘制2D场,X和Y是我的X和Y尺寸,而C是我想用pcolormesh绘制的矩阵,我所要做的就是:(请注意,下面的代码是我的实际代码大大简化)

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.cm import get_cmap 
number_of_ticks=10 # Insert number of ticks wanted 

#'Insert here a non pertinent section of 1000 lines of code where I Acess the data to my ' 
#'variable C and my dimensions X and Y, 
#'All we need to know is that X and Y are arrays, and C is a Matrix' 

fig,ax = plt.subplots(1,1,figsize=(15,12),squeeze= False) 
given_positions=np.linspace(min(X),max(X),number_of_ticks) 
# Un necessary section here where I create a new array 'axticklatlonname', 
#wich are the names for the 10 ticks that I created earlier with given_positions 
new_tick_location=given_positions 
ax.set_xticks(new_tick_locations) 
ax.set_xticklabels(axticklatlonname) 
mycmap=get_cmap('Jet') 
mycmap.set_over('b') 
col=ax.pcolormesh(X,Y,C,norm=LogNorm(vmin=-60,vmax=10),cmap=mycmap,shading='flat') 
cbar=fig.colorbar(col,ax=ax) 
cbar.ax.set_ylabel('TT') 
cbar_ax=fig.add_axes 

直到现在,它的工作原理,它产生了我想要的结果。

我的问题是,我的维Y实际上也是一个变量;我的身高取决于我在X轴上的位置。

这意味着该图将改变,因为C矩阵中的Y位置对于每个X位置不再是恒定的。

我解决了这个问题,通过将我的Y数组更改为矩阵,并通过将我的X数组更改为具有Y等价X数组的矩阵;现在

Xmatrix=np.tile(X(len(Y),1)) 

我可以积pcolormesh:

col=ax.pcolormesh(Xmatrix,Ymatrix,C,norm=LogNorm(vmin=-60,vmax=10),cmap=mycmap,shading='flat') 

现在的问题是,我不能指定我的X刻度位置。

我试图做获取默认的X刻度位置值: ax.get_position() 而且我得到了作为输出B-盒([0.125,0.244],[0.9,0.324])) 这不告诉我我该如何指定我的Xtick位置。

有人知道如何处理Bbox,或简单的方法来指定这些刻度?

回答

0

我终于找到了答案,

其实很简单。 当X和Y是矩阵时,pcolormesh实际上以索引的形式看到轴。

我只需要在[0,len(X)]

之间设置我的X tick位置,而不是在[min(X),max(X)