Scatter a numpy 2D array in matplotlib

I have a 4x4 dataset like

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

Now I want to split this array into a 2D plot.

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

+4
source share
1 answer

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 render a 4x4 array, you can use matshow

plt.matshow(data)
plt.show()
+8
source

Source: https://habr.com/ru/post/1588955/


All Articles