matplotlib get bitmap from a scatter plot

I have coordinates of some points that I need to plot and then convert plot to black & white bitmap:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from PIL import Image

plt.scatter(x,y)
plt.tight_layout()
fig1 = plt.gcf()
plt.show()

enter image description here

type(fig1)
matplotlib.figure.Figure

How to get from this figure black & white bitmap as numpy array similar to this one:

side = 5
image = np.random.choice([0, 1], size=side*side,  p=[.1, .9])
image = image.reshape(side,side)
image = np.expand_dims(image, axis=-1)
print("image.shape: ",image.shape)
plt.imshow(image, cmap=plt.get_cmap('gray'))

image.shape:  (5, 5, 1)

enter image description here

print(image.reshape(side,side))

[[1 1 1 0 1]
 [1 1 1 1 1]
 [1 0 1 1 0]
 [1 1 1 1 0]
 [1 1 1 1 1]]

Update 1

I need also to get resulting bitmap as a numpy array. How to get it?

In case I use solution given by Zephyr:

fig, ax = plt.subplots(figsize = (5,5))
ax.hist2d(x, y, cmap = 'Greys', cmin = 0, cmax = 1)
plt.show()

I get image different from the scatter plot. And they should be similar:

hist2d solution

Scatter plot

Answer

First of all, I generate random N points within range (x_min, x_max) and (y_min, y_max):

np.random.seed(42)

N = 10
x_min = 0
x_max = 40
y_min = -20
y_max = 20

x = np.random.uniform(x_min, x_max, N)
y = np.random.uniform(y_min, y_max, N)

Then I prepare:

  • a grid (the bitmap) of (size, size) dimension
  • two vectors x_grid and y_grid which resample (x_min, x_max) and (y_min, y_max) in size + 1 points, so size inverval: one interval for each grid cell
size = 10
grid = np.zeros((size, size))
x_grid = np.linspace(x_min, x_max, size + 1)
y_grid = np.linspace(y_min, y_max, size + 1)

Then I loop over each grid cells; in each iteration I check if there is at least 1 point of (x, y) which stay within limits of that cell. If so, I set the correspondent value of grid to 1:

for i in range(size):
    for j in range(size):
        for x_i, y_i in zip(x, y):
            if (x_grid[i] < x_i <= x_grid[i + 1]) and (y_grid[j] < y_i <= y_grid[j + 1]):
                grid[i, j] = 1
                break

Resulting numpy matrix:

[[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]

Complete Code

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(42)

N = 10
x_min = 0
x_max = 40
y_min = -20
y_max = 20

x = np.random.uniform(x_min, x_max, N)
y = np.random.uniform(y_min, y_max, N)

size = 10
grid = np.zeros((size, size))
x_grid = np.linspace(x_min, x_max, size + 1)
y_grid = np.linspace(y_min, y_max, size + 1)

for i in range(size):
    for j in range(size):
        for x_i, y_i in zip(x, y):
            if (x_grid[i] < x_i <= x_grid[i + 1]) and (y_grid[j] < y_i <= y_grid[j + 1]):
                grid[i, j] = 1
                break

fig, ax = plt.subplots(1, 2, figsize = (10, 5))

ax[0].scatter(x, y)
ax[0].set_xlim(x_min, x_max)
ax[0].set_ylim(y_min, y_max)
ax[0].grid()
ax[0].set_xticks(x_grid)
ax[0].set_yticks(y_grid)

ax[1].imshow(grid.T, cmap = 'Greys', extent = (x_min, x_max, y_min, y_max))
ax[1].invert_yaxis()

plt.show()

enter image description here

NOTE

Pay attention to the fact that in ax.imshow you need to transpose the matrix (grid.T) and then invert y axis in order to be able to compare the ax.imshow with ax.scatter.
If you want grid matrix to match ax.imshow, then you need to rotate it counterclockwise by 90°:

grid = np.rot90(grid, k=1, axes=(0, 1))

Rotated grid, which correspond to the above plot:

[[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
 [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 1. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]]