簡體   English   中英

Python matlibplot:用圓圈(相同的半徑,不同的顏色)可視化整數矩陣(-1,0或1)

[英]Python matlibplot: visualizing a matrix of integers (-1, 0 or 1) with circles (same radius, different color)

我有一個方形矩陣填充-1,0或1.我想用相同半徑的球體或圓圈可視化這個矩陣。 半徑,確實根本不重要。 但是,這些圓圈必須根據矩陣單元的數量具有不同的顏色。

例如:10 x 10矩陣 - 平面上> 100個圓,10行x 10列位置(2,9)中圓的顏色取決於位置(2,9)中的矩陣數。

謝謝!

我認識的人告訴我使用matlibplot,但我是Python的新手,我有很多問題!

這就是我現在所做的:{`

import numpy as np
#from implementations import *
#from config import *
import matplotlib.pyplot as plt

A = np.array([
    [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],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
    [0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
    [0, 1, 2, -1, -1,-1, 2, 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, 0, 0],
])

rows=len(A) # finding number rows of lattice, so no need to worry about it!
columns=len(A[0]) # finding number columns of lattice, so no need to worry about it!

fig, ax = plt.subplots()

for i in range(rows):
      for j in range(columns):
          if A[i][j]==-1:
              circle1 = plt.Circle((i*4, j*4), 2, color='blue')
              fig = plt.gcf()
              ax = fig.gca()
              ax.add_artist(circle1)
          if A[i][j]== 1:
              circle2 = plt.Circle((i*4, j*4), 2, color='yellow')
              fig = plt.gcf()
              ax = fig.gca()
              ax.add_artist(circle2)
      `}

這是使用散點矩陣matplotlib代碼:

# Imports
import matplotlib.pyplot as plt
from itertools import chain

# Create plot
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

# Here is our matrix. Note, that it is expected to be rectangular!
matrix = [
    [0, 1, 0,1],
    [-1,1, 0,0],
    [1,-1,-1,1],
]

# Get X-length
X = len(matrix[0])

# Get Y-length
Y = len(matrix)

# Construct grids for scatter
x_grid = list(range(X)) * Y  # 1,2,3,4,1,2,3,4...
y_grid = [y for y in range(Y) for _ in range(X)]  # 1,1,1,1,2,2,2,2...

# Flatten the matrix because ax.scatter uses flat arrays
matrix_grid = list(chain(*matrix))

plt.scatter(
    x_grid,              # X-grid array of coordinates
    y_grid,              # Y-grid array of coordinates
    c=matrix_grid,       # Our flatten matrix of -1/0/1s
    cmap='gist_rainbow'  # Color map - defines colors
)

您可以直接使用散點,如下所示:

import numpy as np
import matplotlib.pyplot as plt

A = np.array([
    [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],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
    [0, 1, 2, -1, -1,-1, 2, 1, 0, 0],
    [0, 1, 2, -1, -1,-1, 2, 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, 0, 0],
])

X,Y = np.meshgrid(np.arange(A.shape[1]), np.arange(A.shape[0]))
plt.scatter(X.flatten(), Y.flatten(), c=A.flatten())

plt.show()

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM