1

I have a numpy with each row containing x, y pairs and I want to display a scatter plot without using a for loop so I used the following approach using pandas:

def visualize_k_means_output(self, centroids):
    fig, ax = plt.subplots()
    self.visualize_box_relative_sizes()
    frame = pd.DataFrame(centroids, columns=['X', 'Y'])
    ax.scatter(frame['X'], frame['Y'], marker='*', s=200, c='black')

The question is how to extract the first item as x and the second item as y without using a for loop for example:

ax.scatter(x=[item[0] for item in centroids], y=[item[1] for item in centroids], ...)
0

1 Answer 1

1

If I understood correctly, you want to slice your numpy array:

x = centroids[:, 0]
y = centroids[:, 1]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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