为什么在绘制2d数组时matplotlib等高线图失败

时间:2015-11-21 22:35:44

标签: python numpy matplotlib

我试图使用contourf图绘制一些结果。 X,Y和Z数据都是2d numpy数组。我认为这就像将这些数组传递给contourf函数一样简单。但是,我收到一个错误:

TypeError: 'list' object is not callable

当我执行我的代码时,我不确定为什么。如图所示,我可以得到Z数据的imshow图,但随后的contourf图失败

import matplotlib.pyplot as plt
import numpy as np

def lowpass_resp(frq, tau, expon):
    return 1/np.sqrt(1+(2.0*np.pi*frq*tau)**expon)

def getcoef(rh, typ, bas, ex):
    if typ == 'L':
        coef = bas + ex*rh
    else:
        coef = bas*np.exp(rh*ex)
    return coef   

Q = [('E', 2.1989, 0.0138,  'L', 1.6139, 0.0196),
     ('E', 4.8791, 0.003,   'L', 2.0646, 0.0058),
     ('E', 4.1166, 0.0069,  'L', 2.4588, 0.0011),
     ('E', 4.103, 0.0123,   'L', -21.513, 0.4472),
     ('L', -1.3328, 0.1462, 'L', -22.194, 0.459),
     ('L', 3.0807, 0.0407,  'L', 2.2316, 0.0065),
     ('E', 0.0184, 0.0627,  'L', 3.4733, -0.0205),
     ('E', 0.0527, 0.0454,  'L', 3.2077, -0.0133),
     ('E', 0.1319, 0.0555,  'L', 0.7569, 0.027)]


frq =  np.arange(-50,10)/10.0
frq = 10**frq
rhx = np.arange(10,110, 10)

frq,rhx = np.meshgrid(frq,rhx)

adt = np.dtype=[('tau_type', 'S1'), ('tau_base', 'f'), ('tau_gain', 'f'),('exp_type', 'S1'), ('exp_base', 'f'), ('exp_gain', 'f')]

qcoef = np.array([x for x in Q],dtype=adt)

ix = 0
bcoef = getcoef(rhx, qcoef['tau_type'][ix], qcoef['tau_base'][ix], qcoef['tau_gain'][ix] )
ecoef = getcoef(rhx, qcoef['exp_type'][ix], qcoef['exp_base'][ix], qcoef['exp_gain'][ix] )
tf = lowpass_resp(frq, bcoef, ecoef)

plt.figure()
plt.imshow(tf)
plt.show()

plt.figure()
plt.contourf(frq,rhx,tf)
plt.show()

完整的跟踪如下:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/Users/rclement/Dropbox/Code/python/junktest.py", line 69, in <module>
    plt.contourf(frq,rhx,tfc)
  File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2718, in contourf
    ret = ax.contourf(*args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 5340, in contourf
    return mcontour.QuadContourSet(self, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\contour.py", line 1429, in __init__
    ContourSet.__init__(self, ax, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\contour.py", line 875, in __init__
    self._process_args(*args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\contour.py", line 1442, in _process_args
    x, y, z = self._contour_args(args, kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\contour.py", line 1512, in _contour_args
    self.zmax = ma.maximum(z)
  File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 5954, in __call__
    return self.reduce(a)
  File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 5971, in reduce
    target = target.filled(self.fill_value_func(target)).view(type(target))
  File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 3496, in filled
    fill_value = _check_fill_value(fill_value, self.dtype)
  File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 401, in _check_fill_value
    ndtype = np.dtype(ndtype)
TypeError: 'list' object is not callable

1 个答案:

答案 0 :(得分:1)

在这一行:

adt = np.dtype=[('tau_type', 'S1'), ...

您已将np.dtype重新定义为列表而非预期函数。将此行更改为

adf = [('tau_type', 'S1'), ...

并且错误将消失。