โดยใช้ Pandas เราสามารถสร้าง DataFrame และสร้าง Figure และ Axis ได้ หลังจากนั้น เราสามารถใช้เมธอด scatter เพื่อวาดจุดได้
ขั้นตอน
สร้างลิสต์ของนักเรียน, คะแนนที่ได้รับ, และการเข้ารหัสสีสำหรับแต่ละคะแนน
สร้าง DataFrame ด้วย DataFrame ของ Pandas ด้วยข้อมูลจากขั้นตอนที่ 1
สร้างตัวแปร fig และ ax ด้วยเมธอด subplots โดยค่าเริ่มต้น nrows และ ncols เป็น 1
ตั้งค่าป้ายกำกับแกน X ด้วย
plt.xlabel()ตั้งค่าป้ายกำกับแกน Y ด้วย
plt.ylabel()Scatter plot ของ y ต่อ x พร้อมขนาดและ/หรือสีของเครื่องหมายที่เปลี่ยนแปลงได้
ใช้
plt.show()แสดงกราฟ
ตัวอย่าง
from matplotlib import pyplot as plt
import pandas as pd
no_of_students = [1, 2, 3, 5, 7, 8, 9, 10, 30, 50]
marks_obtained_by_student = [100, 95, 91, 90, 89, 76, 55, 10, 3, 19]
color_coding = ['red', 'blue', 'yellow', 'green', 'red', 'blue', 'yellow', 'green', 'yellow', 'green']
df = pd.DataFrame(dict(students_count=no_of_students, marks=marks_obtained_by_student, color=color_coding))
fig, ax = plt.subplots()
plt.xlabel('Students count')
plt.ylabel('Obtained marks')
ax.scatter(df['students_count'], df['marks'], c=df['color'])
plt.show()
ผลลัพธ์
