matplotlib 히스토그램에서 다른 막대에 대해 다른 색상을 지정하려면 다음 단계를 수행할 수 있습니다. -
단계
-
Figure 크기를 설정하고 서브플롯 사이와 주변의 패딩을 조정합니다.
-
Figure와 서브플롯 세트를 생성합니다.
-
100개의 샘플 데이터가 있는 임의의 데이터로 히스토그램을 플로팅합니다.
-
빈 수 범위에서 반복하고 각 막대에 대해 임의의 면색을 설정합니다.
-
그림을 표시하려면 show()를 사용하세요. 방법.
예시
import numpy as np
import matplotlib.pyplot as plt
import random
import string
# Set the figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Figure and set of subplots
fig, ax = plt.subplots()
# Random data
data = np.random.rand(100)
# Plot a histogram with random data
N, bins, patches = ax.hist(data, edgecolor='black', linewidth=1)
# Random facecolor for each bar
for i in range(len(N)):
patches[i].set_facecolor("#" + ''.join(random.choices("ABCDEF" + string.digits, k=6)))
# Display the plot
plt.show() 출력
다음 출력을 생성합니다 -
