matplotlib 从散点图中获取位图

matplotlib get bitmap from a scatter plot

我有一些点的坐标,我需要绘制然后将绘图转换为黑白位图:

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()

type(fig1)
matplotlib.figure.Figure

如何从这个图中得到黑白位图作为类似于这个的 numpy 数组:

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)

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]]

更新 1

我还需要将生成的位图作为 numpy 数组。如何获得?

如果我使用 Zephyr 提供的解决方案:

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

我得到的图像与散点图不同。它们应该是相似的:

首先,我在 (x_min, x_max)(y_min, y_max) 范围内生成随机 N 点:

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)

那我准备:

  • 一个 grid (size, size) 维度的(位图)
  • 两个向量x_gridy_gridsize + 1点重新采样(x_min, x_max)(y_min, y_max),所以size inverval:一个间隔每个 grid 个细胞
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)

然后我遍历每个 grid 个单元格;在每次迭代中,我检查是否至少有 1 个 (x, y) 点在该单元格的范围内。如果是这样,我将 grid 对应的值设置为 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

生成的 numpy 矩阵:

[[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.]]

完整代码

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()

注意

请注意,在 ax.imshow 中,您需要转置矩阵 (grid.T),然后反转 y 轴,以便能够将 ax.imshowax.scatter.
如果想让grid矩阵匹配ax.imshow,那么需要逆时针旋转90°:

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

旋转grid,对应上图:

[[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.]]

您可以创建一个网格并使用它来定义地图,其中最近的点将为白色。我尝试使用随机数据,范围从 0 到 1:

import matplotlib.pyplot as plt
import numpy as np

n_points = 10
# create random coordinates
x, y = np.random.rand(n_points,2).T
fig, ax = plt.subplots()
ax.scatter(x,y)
ax.set_xlim([0,1])
ax.set_ylim([0,1])
ax.set_aspect(1.0)

# create a grid
grid_points = 10
grid_x = np.linspace(0,1,grid_points)
grid_y = grid_x.copy()

# initiate array of ones (white)
image = np.ones([grid_points, grid_points])
for xp, yp in zip(x,y):
    # selecing the closest point in grid
    index_x = np.argmin(np.abs(xp - grid_x))
    index_y = np.argmin(np.abs(yp - grid_y))
    # setting to black
    image[index_x,index_y] = 0
    
# you need to transpose it so x is represented
# by the columns and y by the rows
fig, ax = plt.subplots()
ax.imshow(
    image.T,
    origin='lower',
    cmap=plt.get_cmap('gray'))

请注意,最接近的不一定总是好的。使用更精细的网格会变得更好。