Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Matplotlib에서 행렬 값과 컬러맵을 표시하는 방법은 무엇입니까?

<시간/>

Matplotlib에서 행렬 값과 컬러맵을 표시하려면 다음 단계를 수행할 수 있습니다. -

  • 그림 크기를 설정하고 서브플롯 사이 및 주변 여백을 조정합니다.
  • 그림과 서브플롯 세트를 생성합니다.
  • 행렬의 최대값과 최소값을 초기화합니다.
  • 2D 행렬 또는 배열의 값을 색상으로 구분된 이미지로 표시합니다.
  • 색상 코드 이미지의 각 셀을 반복하고 중앙에 값을 배치합니다.
  • 그림을 표시하려면 show()를 사용하세요. 방법.

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig, ax = plt.subplots()
min_val, max_val = 0, 5
matrix = np.random.randint(0, 5, size=(max_val, max_val))
ax.matshow(matrix, cmap='ocean')

for i in range(max_val):
   for j in range(max_val):
      c = matrix[j, i]
      ax.text(i, j, str(c), va='center', ha='center')

plt.show()

출력

Matplotlib에서 행렬 값과 컬러맵을 표시하는 방법은 무엇입니까? Matplotlib에서 행렬 값과 컬러맵을 표시하는 방법은 무엇입니까?