2

我正在尝试设置使用 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()

生成这样的情节

在此处输入图像描述 以前我一直在使用 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.

我现在正在努力想其他方法可以做到这一点。

有什么想法/建议吗?

4

1 回答 1

1

您可以通过在调用中去掉额外的括号来消除错误contour

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

如果这不能为您提供所需的绘图,请尝试xlim直接ylim设置轴限制。

于 2013-07-22T18:25:14.153 回答