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

วิธีพลอตตัวแปร Categorical (หมวดหมู่) ใน Matplotlib


เพื่อพลอตตัวแปร categorical (หมวดหมู่) ใน Matplotlib

  • ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
  • สร้าง dictionary ข้อมูล
  • ดึง keys และ values จาก dictionary
  • สร้าง figure และ subplots
  • พลอต bar, scatter และ plot ด้วย names และ values
  • แสดงรูปด้วย show()

ตัวอย่าง

import matplotlib.pyplot as plt

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

data = {'apple': 10, 'orange': 15, 'lemon': 5}
names = list(data.keys())
values = list(data.values())

fig, axs = plt.subplots(1, 3)
axs[0].bar(names, values)
axs[1].scatter(names, values)
axs[2].plot(names, values)
plt.show()

ผลลัพธ์

วิธีพลอตตัวแปร Categorical (หมวดหมู่) ใน Matplotlib