0

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.

  • 1
    `np.matrix` is a type object, which you cannot index. What are you trying to do there? – OneCricketeer Nov 14 '16 at 19:39
  • I just want to give Z the value of that point. So then how to I do? – Yaofeng Wang Nov 14 '16 at 19:41
  • Did you mean to use `matrix` instead of `np.matrix`? – OneCricketeer Nov 14 '16 at 19:41
  • @YaofengWang -- you're acting like `X+40` and `Y+40` are scalars -- they aren't. They're arrays which are constructed by adding 40 to every element of the arrays returned by `np.meshgrid`. So it's very unclear how you're trying to give `Z` a value of a specific point when you don't have a specific point... – mgilson Nov 14 '16 at 19:43

2 Answers2

0

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.

lucasnadalutti
  • 5,818
  • 1
  • 28
  • 48
0

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)
Jeff Mandell
  • 863
  • 7
  • 16