我在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] ])
矩陣

我想在這個陣列中的相同的值之間畫線,這樣:
最后,我想在這個陣列中的相同值之間畫線。

我在Python中尋找matplotlib和turtle庫,但沒有找到繪制這個的方法。
uj5u.com熱心網友回復:
只使用matplotlib :
importmatplotlib.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()
uj5u.com熱心網友回復:
這個答案使用了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 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] :x.append(c) y.append(r)
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 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] :x.append(c) y.append(r)
plt.plot(y, x,
color='red',
linestyle='solid',
marker='o'。
markerfacecolor='black'。
markeredgecolor='black'。
markersize=12)
r =1。
輸出
uj5u.com熱心網友回復:
使用烏龜圖形,我們可以這樣來處理這個問題:
from turtle import Screen, Turtle
import numpy as np
NEIGHBORS = [(0, 1), (1, 0) ]
矩陣 = 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') #有方法可以更快。
turtle.color('red)
turtle.pensize(4)
turtle.penup()
for row in range(高度)。
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。
通過。
turtle.goto(col, row)
turtle.dot(15, 'black')
screen.exitonclick()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/328923.html
標籤:
上一篇:不了解病情



