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

วิธีสร้าง Waffle Chart ใน Python Matplotlib

Waffle Chart เป็นเทคนิคการแสดงผลที่น่าสนใจ ใช้แสดงความคืบหน้าเป้าหมาย โดยทั่วไปจะสร้างผ่าน FigureClass=Waffle เมื่อสร้าง Figure ใหม่

ขั้นตอน

  • สร้าง DataFrame ของ Pandas จาก Dictionary
  • สร้าง Figure ใหม่ด้วย FigureClass=Waffle, rows=5, values=df.price และ labels=df.books
  • แสดงกราฟด้วย show()

ตัวอย่างโค้ด

import pandas as pd
import matplotlib.pyplot as plt
from pywaffle import Waffle

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

data = {
    'books': ['physics', 'chemistry', 'math', 'english', 'hindi'],
    'price': [80, 87, 89, 56, 39]
}
df = pd.DataFrame(data)

fig = plt.figure(
    FigureClass=Waffle,
    rows=5,
    values=df.price,
    labels=list(df.books)
)
plt.show()

ผลลัพธ์

วิธีสร้าง Waffle Chart ใน Python Matplotlib