在 python 中的矩阵中的相同值之间绘制网格线

draw grid lines between same values in a matrix in python

我在 python 中有一个 np.array,如下所示:

mat = np.array([[0.2, 0.2, 0.1, 0.1, 0.1],
      [0.2, 0.2, 0.1, 0.1, 0.0],
      [0.2, 0.2, 0.1, 0.1, 0.0],
      [0.2, 0.1, 0.1, 0.0, 0.0]])
mat

我想在此数组中的相同值之间画线,这样:

最后我想要一个这样的情节:

我在 python 中寻找了 matplotlib 和 turtle 库,但找不到绘制它的方法。

这个答案使用了itertools.groupby。我们首先翻转数组,这样当我们遍历它时,我们从下到上,对于每一行,我们使用 groupby 来捕获连续的组及其大小。基于第一个循环构建点和线。

然后我们转置数组并遍历列,做基本相同的事情,但交换 x 和 y。

您几乎肯定可以将其转换为一个函数,多加考虑,基本上可以处理行和列,而无需几乎重复代码。

import numpy as np
from itertools import groupby
import matplotlib.pyplot as plt

mat = np.array([[0.2, 0.2, 0.1, 0.1, 0.1],
      [0.2, 0.2, 0.1, 0.1, 0.0],
      [0.2, 0.2, 0.1, 0.1, 0.0],
      [0.2, 0.1, 0.1, 0.0, 0.0]])

mat = np.flip(mat,axis=0)

for r in range(mat.shape[0]-1,-1,-1):
    c = 0
    l = [(key, len(list(it))) for (key, it) in groupby(mat[r])]

    for p in l:
        if p[1]==1:

            plt.plot(c,r, color='red',
                     linestyle='solid',
                     marker='o',
                     markerfacecolor='black', 
                     markeredgecolor='black',
                     markersize=12)
            c+=1
        else:
            x = []
            y = []
            for i in range(p[1]):
                x.append(c)
                y.append(r)
                c+=1
            plt.plot(x, y, color='red',
                     linestyle='solid', 
                     marker='o',
                     markerfacecolor='black', 
                     markeredgecolor='black', 
                     markersize=12)
    r+=1
    
mat = np.transpose(mat)

for r in range(mat.shape[1],-1,-1):
    c = 0
    l = [(key, len(list(it))) for (key, it) in groupby(mat[r])]
    for p in l:
        if p[1]==1:
            plt.plot(r,c, 
                     color='red', 
                     linestyle='solid', 
                     marker='o',
                     markerfacecolor='black',
                     markeredgecolor='black',
                     markersize=12)
            c+=1
        else:
            x = []
            y = []
            for i in range(p[1]):
                x.append(c)
                y.append(r)
                c+=1
            plt.plot(y, x,
                     color='red', 
                     linestyle='solid', 
                     marker='o',
                     markerfacecolor='black', 
                     markeredgecolor='black', 
                     markersize=12)
    r+=1

输出

使用海龟图形,我们可以这样解决问题:

from turtle import Screen, Turtle
import numpy as np

NEIGHBORS = [(0, 1), (1, 0)]

matrix = np.array([
    [0.2, 0.2, 0.1, 0.1, 0.1],
    [0.2, 0.2, 0.1, 0.1, 0.0],
    [0.2, 0.2, 0.1, 0.1, 0.0],
    [0.2, 0.1, 0.1, 0.0, 0.0]
])

height, width = matrix.shape

screen = Screen()
screen.setup(width * 100, height * 100)
screen.setworldcoordinates(-0.5, height - 0.5, width - 0.5, -0.5)

turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')  # there are ways to go even faster
turtle.color('red')
turtle.pensize(4)
turtle.penup()

for row in range(height):
    for col in range(width):
        for dx, dy in NEIGHBORS:
            try:
                if matrix[row, col] == matrix[row + dy, col + dx]:
                    turtle.goto(col, row)
                    turtle.pendown()
                    turtle.goto(col + dx, row + dy)
                    turtle.penup()
            except IndexError:
                pass

        turtle.goto(col, row)
        turtle.dot(15, 'black')

screen.exitonclick()

仅使用 matplotlib :

import matplotlib.pyplot as plt

mat = [[0.2, 0.2, 0.1, 0.1, 0.1],
      [0.2, 0.2, 0.1, 0.1, 0.0],
      [0.2, 0.2, 0.1, 0.1, 0.0],
      [0.2, 0.1, 0.1, 0.0, 0.0]]

def line(x,y):
     plt.plot(x,y,marker = 'o',color = 'red',
     markerfacecolor='black',markeredgecolor='black')

def draw(mat):
     for i in range(len(mat)):
          for k in range(len(mat[i])-1):
               if mat[i][k] == mat[i][k+1] :
                    line([k,k+1],[len(mat)-i,len(mat)-i])
     for i in range(len(mat)-1):
          for k in range(len(mat[i])):
               if mat[i][k] == mat[i+1][k]:
                    line([k,k],[len(mat)-i,len(mat)-i-1])

draw(mat)
plt.show()