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

Matplotlib로 100% 누적 영역 차트를 만드는 방법은 무엇입니까?

<시간/>

Matplotlib를 사용하여 100% 누적 영역 차트를 만들려면 다음 단계를 수행할 수 있습니다. -

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

  • 연도 목록을 만드십시오.

  • 해당 연도의 인구 목록으로 사전을 만드십시오.

  • Figure와 서브플롯 세트를 생성합니다.

  • 누적 면적 플롯을 그립니다.

  • 그림의 ''왼쪽 상단'' 위치에 범례를 배치합니다. .

  • 제목, xlabel 설정 및 ylabel .

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

예시

import matplotlib.pyplot as plt

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

year = [1950, 1960, 1970, 1980, 1990, 2000, 2010, 2018]

population_by_continent = {
   'africa': [228, 284, 365, 477, 631, 814, 1044, 1275],
   'americas': [340, 425, 519, 619, 727, 840, 943, 1006],
   'asia': [1394, 1686, 2120, 2625, 3202, 3714, 4169, 4560],
   'europe': [220, 253, 276, 295, 310, 303, 294, 293],
   'oceania': [12, 15, 19, 22, 26, 31, 36, 39],
}

fig, ax = plt.subplots()
ax.stackplot(year, population_by_continent.values(), labels=population_by_continent.keys())

ax.legend(loc='upper left')
ax.set_title('World population')

ax.set_xlabel('Year')
ax.set_ylabel('Number of people (millions)')

plt.show()

출력

Matplotlib로 100% 누적 영역 차트를 만드는 방법은 무엇입니까?