8

I have a 4x4 data array like

data = np.array([[0,1,1,1], [1,0,0,1], [1,1,1,0], [0,0,0,1]])

Now I want to scatter this array on a 2D plot.

If data[i,j] is equal to 1, there should be a colored spot at point (x,y)=(i,j). I've tried with scatter plot in matplotlib, but somehow couldn't make it work.

1 Answer 1

14

You can do it with

import numpy as np
import matplotlib.pyplot as plt

data = np.array([[0,1,1,1], [1,0,0,1], [1,1,1,0], [0,0,0,1]])

# get the indices where data is 1
x,y = np.argwhere(data == 1).T

plt.scatter(x,y)
plt.show()

However, when you just want to visualize the 4x4 array you can use matshow

plt.matshow(data)
plt.show()
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.