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

Pandas 및 Matplotlib를 사용하여 해치 막대를 어떻게 플롯합니까?

<시간/>

해치 막대를 플롯하려면 Pandas를 사용하여 다음 단계를 수행할 수 있습니다. -

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

  • 두 개의 열이 있는 Pandas를 사용하여 데이터 프레임을 만듭니다.

  • 현재 Figure에 하위 플롯 배열로 축을 추가합니다.

  • kind="bars"로 줄거리 만들기 이름으로 클래스.

  • 해치 목록을 만드십시오.

  • bars.patches를 사용하여 막대 패치를 가져옵니다. .

  • 막대 반복 패치 및 각 패치의 해치를 설정합니다.

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

예시

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

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

df = pd.DataFrame(np.random.rand(5, 2), columns=['a', 'b'])
ax = plt.figure().add_subplot(111)
bars = df.plot(ax=ax, kind='bar')
hatches = ["*", "/", "o", "x"]

for patch in bars.patches:
   patch.set_hatch(hatches[np.random.randint(10)%len(hatches)])

plt.show()

출력

Pandas 및 Matplotlib를 사용하여 해치 막대를 어떻게 플롯합니까?