Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

จะสร้างแผนผังพื้นที่แบบซ้อน 100% ด้วย Matplotlib ได้อย่างไร


ในการสร้างแผนผังพื้นที่แบบซ้อน 100% ด้วย Matplotlib เราสามารถทำตามขั้นตอนต่อไปนี้ -

  • กำหนดขนาดรูปและปรับช่องว่างภายในระหว่างและรอบๆ แผนผังย่อย

  • สร้างรายชื่อปี

  • ทำพจนานุกรมพร้อมรายชื่อประชากรในแต่ละปี

  • สร้างร่างและชุดแผนย่อย

  • วาดแผนผังพื้นที่แบบเรียงซ้อน

  • วางคำอธิบายบนร่างที่ตำแหน่ง ''บนซ้าย'' .

  • ตั้งค่า ชื่อเรื่อง 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()

ผลลัพธ์

จะสร้างแผนผังพื้นที่แบบซ้อน 100% ด้วย Matplotlib ได้อย่างไร