0

I am trying to make a scatter plot with the list of coordinates I have. I'm just confused how to separate these coordinates into x and y so that I can plug it into the plt.scatter() function, especially considering it is a nested array of arrays.

   array([array([1., 0.]), array([ 0., -1.]), array([-1.,  0.]),
   array([-1., -1.]), array([ 1., -1.]), array([ 0., -2.]),
   array([-2.,  0.]), array([2., 0.]), array([-2., -1.]),
   array([ 2., -1.]), array([-1., -2.]), array([ 1., -2.]),
   array([-2., -2.]), array([ 2., -2.]), array([ 0., -3.]),
   array([3., 0.]), array([-3.,  0.]), array([ 1., -3.]),
   array([-1., -3.]), array([ 3., -1.]), array([-3., -1.]),
   array([-2., -3.]), array([ 3., -2.]), array([-3., -2.]),
   array([ 2., -3.]), array([-4.,  0.]), array([4., 0.]),
   array([-4., -1.]), array([ 4., -1.]), array([-3., -3.]),
   array([ 3., -3.]), array([ 4., -2.]), array([-4., -2.]),
   array([ 4., -3.]), array([-4., -3.]), array([-1., -5.]),
   array([ 1., -5.]), array([-5., -1.]), array([-5., -2.]),
   array([-5., -3.]), array([ 3., -5.]), array([-3., -5.]),
   array([-6.,  0.]), array([-6., -1.]), array([-6., -2.]),
   array([-6., -3.]), array([-5., -5.]), array([-7., -1.]),
   array([ 1., -7.]), array([-1., -7.]), array([-7., -2.]),
   array([-3., -7.]), array([-7., -3.]), array([ 3., -7.]),
   array([-7., -5.]), array([-5., -7.]), array([-1., -9.]),
   array([ 1., -9.]), array([ 3., -9.]), array([-3., -9.]),
   array([-7., -7.]), array([-5., -9.]), array([  1., -11.]),
   array([ -1., -11.]), array([ -3., -11.]), array([  3., -11.]),
   array([-7., -9.]), array([ -5., -11.]), array([ -7., -11.])],
  dtype=object)
5
  • 1
    Does x,y = np.array(my_data).T; plt.scatter(x,y) help? Commented Apr 22, 2022 at 21:38
  • Maybe np.array(my_data, dtype=float).T to get rid of that pesky dtype=object? Commented Apr 22, 2022 at 21:42
  • Does np.stack help? Commented Apr 22, 2022 at 21:50
  • When I try transposing, it says 'ValueError: too many values to unpack (expected 2)' Commented Apr 22, 2022 at 21:52
  • 1
    Pay close attention to what you have. What's the shape? The dtype is object. To make a scatter plot you need a (n,2) array, with float dtype. If the shape is 1d (e.g. (69,)), then np.stack(arr) should work. Transposing a 1d object array does nothing. Commented Apr 22, 2022 at 22:12

1 Answer 1

1
import numpy as np
import matplotlib.pyplot as plt

arr = np.array([
    0,
    np.array([0.,  3.]),
    np.array([2., 4.]),
    np.array([-3., -1.]),
    np.array([-4.,  2.]),
    np.array([2., 0.]),
    np.array([-2., -2.])
], dtype=object)[1:]
print(repr(arr))

prints:

array([array([0., 3.]), array([2., 4.]), array([-3., -1.]),
       array([-4.,  2.]), array([2., 0.]), array([-2., -2.])],
      dtype=object)

then you can use np.stack (as others have already suggested in the comments) to convert arr to a 2d-array:

arr = np.stack(arr)
print(repr(arr))

which prints:

array([[ 0.,  3.],
       [ 2.,  4.],
       [-3., -1.],
       [-4.,  2.],
       [ 2.,  0.],
       [-2., -2.]])

and is much easier to plot:

plt.scatter(arr[:, 0], arr[:, 1])
Sign up to request clarification or add additional context in comments.

3 Comments

This didn't work for me, it says: 'IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed'
MIchael, when you print your arr, does it show nesting arrays as the OP's does? If it is a 2d float array, then you did not replicate his case,
hpaulj thanks for pointing this out. It indeed did not replicate OP's case, but now I think it does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.