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

matplotlib.pyplot에서 hist2d와 함께 컬러바를 어떻게 사용합니까?

<시간/>

hist2d와 함께 컬러바를 사용하려면 matplotlib.pyplot에서 , 다음 단계를 수행할 수 있습니다.

단계

  • Figure 크기를 설정하고 서브플롯 사이와 주변의 패딩을 조정합니다.

  • 변수 "N" 초기화 샘플 데이터의 수에 대해.

  • x 만들기 및 y numpy를 사용하는 데이터 포인트.

  • subplots()를 사용하여 Figure와 서브플롯 세트 생성 방법.

  • hist2D()를 사용하여 2D 히스토그램 플롯을 만듭니다.

  • hist2d용 컬러바 만들기 스칼라 매핑 가능 인스턴스.

  • 그림을 표시하려면 Show()를 사용하세요. 방법.

예시

from matplotlib.colors import LogNorm
import matplotlib.pyplot as plt
import numpy as np

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Number of sample data
N = 1000

# Create x and y data points
x = np.random.rand(N)
y = np.random.rand(N)

fig, ax = plt.subplots()

# 2D histogram plot with x and y
hh = ax.hist2d(x, y, bins=40, norm=LogNorm())

fig.colorbar(hh[3], ax=ax)

# Display the plot
plt.show()

출력

다음 출력을 생성합니다 -

matplotlib.pyplot에서 hist2d와 함께 컬러바를 어떻게 사용합니까?