在不使用xticks的情况下设置2D等高线图的轴值

时间:2013-07-22 16:00:13

标签: python matplotlib

我正在尝试设置使用contour()生成的绘图的x轴和y轴的值,但目前无法按照所希望的方式读取轴外的特定值。

fid_list = []
for fidN in arange(frames):
    offset = fidN * fid_pts 
    current_fid = cmplx_data[offset:offset+fid_pts]
    fid_list.append(current_fid)

fid_mat = fid_list
jres_spec = abs(fftshift(fft2(fid_mat)))
max_val = jres_spec.max()/15
min_val = max_val*0.15
steps = 40
figure()
CS=contour(jres_spec,arange(min_val,max_val,(max_val-min_val)/steps))
show()

生成这样的情节

enter image description here 以前我一直在使用xticks和yticks来设置轴的值,但现在绘图上的确切位置变得很重要,因此能够从轴读取值将非常有用,我无法用x做/ yticks。

在绘制一维光谱时,我使用以下公式使我能够读取x轴

bins = arange(828, -196, -1) #change this so that 0 value occurs at value it's meant to
x = (2000 * bins / 1024.0)/128.0
plot(x, fftshift(fft(fid_list[0])))
plt.gca().invert_xaxis()
show()

并且类似地将其用于我的2D等高线图的y轴

ybins = arange(15, -15, -1)
y = ybins * ((1/(15*10^(-3)))/ 30.0)

但我无法将其整合到我的代码中......

我尝试过使用类似的东西

ybins = arange(15, -15, -1)
y = ybins * ((1/(15*10^(-3)))/ 30.0)
xbins = arange(828, -196, -1) 
x = (2000 * xbins / 1024.0)/128.0

fid_mat = fid_list
jres_spec = abs(fftshift(fft2(fid_mat)))
max_val = jres_spec.max()/15
min_val = max_val*0.15
steps = 40
figure()
CS=contour((x, y, jres_spec),arange(min_val,max_val,(max_val-min_val)/steps))
show()

返回

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/dominicc/<ipython-input-34-28b34c6c069d> in <module>()
      7 bins = arange(828, -196, -1) #change this so that 0 value occurs at value it's meant to
      8 x = (2000 * bins / 1024.0)/128.0
----> 9 CS=contour((x_list, jres_spec),arange(min_val,max_val,(max_val-min_val)/steps))
     10 show()

/usr/lib/pymodules/python2.7/matplotlib/pyplot.pyc in contour(*args, **kwargs)
   2195         ax.hold(hold)
   2196     try:
-> 2197         ret = ax.contour(*args, **kwargs)
   2198         draw_if_interactive()
   2199     finally:

/usr/lib/pymodules/python2.7/matplotlib/axes.pyc in contour(self, *args, **kwargs)
   7379         if not self._hold: self.cla()
   7380         kwargs['filled'] = False
-> 7381         return mcontour.QuadContourSet(self, *args, **kwargs)
   7382     contour.__doc__ = mcontour.QuadContourSet.contour_doc
   7383 

/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
   1110         are described in QuadContourSet.contour_doc.
   1111         """
-> 1112         ContourSet.__init__(self, ax, *args, **kwargs)
   1113 
   1114     def _process_args(self, *args, **kwargs):

/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
    701         if self.origin == 'image': self.origin = mpl.rcParams['image.origin']
    702 
--> 703         self._process_args(*args, **kwargs)
    704         self._process_levels()
    705 

/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in _process_args(self, *args, **kwargs)
   1123             self.zmax = args[0].zmax
   1124         else:
-> 1125             x, y, z = self._contour_args(args, kwargs)
   1126 
   1127             x0 = ma.minimum(x)

/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in _contour_args(self, args, kwargs)
   1167         if Nargs <= 2:
   1168             z = ma.asarray(args[0], dtype=np.float64)
-> 1169             x, y = self._initialize_x_y(z)
   1170             args = args[1:]
   1171         elif Nargs <=4:

/usr/lib/pymodules/python2.7/matplotlib/contour.pyc in _initialize_x_y(self, z)
   1230         '''
   1231         if z.ndim != 2:
-> 1232             raise TypeError("Input must be a 2D array.")
   1233         else:
   1234             Ny, Nx = z.shape

TypeError: Input must be a 2D array.

我现在正在努力想出其他方法。

有任何想法/建议吗?

1 个答案:

答案 0 :(得分:1)

你可以通过在contour的调用中取出额外的括号来消除错误:

CS=contour(x, y, jres_spec,arange(min_val,max_val,(max_val-min_val)/steps))

如果这不能提供您想要的情节,请尝试xlimylim直接设置轴限制。