TypeError 'type' object has no attribute '__getitem__' when drawing a 3D-plot

180 Views Asked by At

Below is my fuction:

def draw3D(draw_tick, matrixArray):
    print "Drawing tick = %d\n" % draw_tick
    matrix = matrixArray[draw_tick - 450]
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    X = np.arange(-40, 40, 1)
    Y = np.arange(-40, 40, 1)
    X, Y = np.meshgrid(X, Y)
    Z = np.matrix[Y+40][X+40]
    surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1,cmap=cm.coolwarm,linewidth=0, antialiased=False)
    ax.set_zlim(-1.01, 1.01)

    ax.zaxis.set_major_locator(LinearLocator(10))
    ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

    fig.colorbar(surf, shrink=0.5, aspect=5)

    plt.show()
    plt.close()

I want to draw a 3D plot with variables x,y,z.

TypeError: 'type' object has no attribute '__getitem__'

This error points to the line of Z:

Z = np.matrix[Y+40][X+40]

I want to store the value of that point (of matrix) into Z

Can anyone help me to solve it?

Lots of thanks!

Update of my question: I have a matrixArray containing hundreds of matrices of 81*81. I want to draw a plot of one matrix in that array. So I declared:

matrix= matrixArray[draw_tick - 450]

to decide the particular one. Then I want to put the matrix location as X & Y, and put the value of the location as Z. But I want my X and Y be from -40 to +40, that is why I'm adding 40 to the two axis.

2

There are 2 best solutions below

1
On

numpy.matrix is a class (and classes in Python are objects that are instances of type), and you are trying to access it as if it was a nested array. You probably want the value in matrix instead.

1
On

From calling help(np.matrix) we get:

 |  Examples
 |  --------
 |  >>> a = np.matrix('1 2; 3 4')
 |  >>> print a
 |  [[1 2]
 |   [3 4]]
 |  
 |  >>> np.matrix([[1, 2], [3, 4]])
 |  matrix([[1, 2],
 |          [3, 4]])
 |  

You must create an instance of the matrix. Perhaps you want to do:

Z = np.matrix(YOUR_ndarray_AS_ARGUMENT)