3

I'm new to this and I'm trying to represent a structured array, npy file as a scatter plot. I'm not entirely sure what my other argument should be. I was thinking that I should span out my values for x and y, but I am not sure.

import matplotlib.pyplot as plt
import numpy as np
import os

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

os.chdir( path )

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')])

plt.title ("MS II data structure")
plt.xlabel(r'$\Omega_{\mu \nu}$')
plt.ylabel(r'$\Omega^{\mu \nu}$')

plt.scatter(data)
plt.show()

Inputting this outputs the error:

TypeError: scatter() takes at least 2 arguments (1 given)

DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
iron2man
  • 1,787
  • 5
  • 27
  • 39
  • what exactly is unclear? A scatter plot always between two vectors. That's what the error message is telling you. – cel Nov 29 '15 at 20:08
  • So a scatter plot cannot be made with this file? – iron2man Nov 29 '15 at 20:09
  • Are you sure that you understood the concept of a scatter plot? You may want to google and look at the results. Is this what you actually want to plot here? – cel Nov 29 '15 at 20:10
  • I have the information marked by #, its (167197,). – iron2man Nov 29 '15 at 20:26
  • Well, a scatter plot requires x and y data. A (x,y) pair creates a point, and all pairs together constitute the scatter plot. If you only have data for one axis, you cannot do a scatter plot. Here is an example of a scatter plot: http://matplotlib.org/examples/shapes_and_collections/scatter_demo.html – Alexander Vogt Nov 29 '15 at 20:27
  • That's unfortunate. The data is from the Millennium-II simulation that contains data on every halo in the simulation at o redshift. Would this data only be usable for just plotting and function making? – iron2man Nov 29 '15 at 20:33

2 Answers2

1

plt.scatter needs at least two arguments (which the error states quite clearly).

If you look into the docs (http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter), you will see this signature:

scatter(x, y, s=20, c=None, marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, hold=None, data=None, **kwargs)

So you need to provide at least an array for each x and y values:

plt.scatter(data['x'], data['y'])

Starting from matplotlib 1.5, you could also use this syntax to access data from a structured array:

plt.scatter('x', 'y', data=data)
MaxNoe
  • 14,470
  • 3
  • 41
  • 46
0

You have a structured array there. Matplotlib does not know what to do with it. As given in the documentation for matplotlib.pyplot.scatter() you need to specify two input arrays x, and y.

From the dtype output, I gather that your structured array has values for 'x', 'y', 'z', 'velx', 'vely', 'velz', and 'm200'. I have no clue what they are, but in order to create a scatter plot, you need to specify two components, e.g.:

plt.scatter(data['x'], data['y'])

Assuming that 'm200' should be mapped to the width of the scatter points, you could use:

plt.scatter(data['x'], data['y'], s=data['m200'])
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68