Search code examples
pythonplotsurfacemplot3dstructured-array

Surface plotting structured array error


I've been trying to represent this structured array in a 3d plot in hopes of later mapping it.

import matplotlib.pyplot as plt
import numpy as np
import os
from mpl_toolkits.mplot3d import Axes3D

path = '/users/username/Desktop/untitled folder/python files/MSII_phasespace/'

os.chdir( path )

fig = plt.figure()
ax = fig.gca( projection='3d')


data = np.load('msii_phasespace.npy',mmap_mode='r')

# data.size: 167197
# data.shape: (167197,)
# data.dtype: dtype([('x', '<f4'), ('y', '<f4'), ('z', '<f4'),
  # ('velx', '<f4'), ('vely', '<f4'), ('velz', '<f4'), ('m200', '<f4')])



u = data[:500]
v = data[:500]

xi = u['x']
yi = v['y']


X, Y = np.meshgrid(xi, yi)
Axes3D.plot_surface(X, Y, data)


plt.show()

Running this led me to this error

unbound method plot_surface() must be called with Axes3D instance as first argument (got memmap instance instead).

I'm not entirely sure what it is asking me. I'm i bit of a beginner with this, so I would appreciate any help i can get. Also, would including a third value of z be applicable?

I've also included the size, shape, and dtype in #.


Solution

  • The call to plot_surface must be made on a specific instance of Axes3D, not on the class. Python instance methods implicitly have a first self parameter that is passed in when you call a method on an object.

    What this means for you is that Axes3D.plot_surface(X, Y, data) should be ax.plot_surface(X, Y, data). The ax object tells Python which set of axes to invoke plot_surface() on.