Sử dụng Pandas, chúng ta có thể tạo dataframe và tạo biến figure và axes bằng subplot(). Sau đó, dùng phương thức ax.scatter() để lấy biểu đồ cần thiết.
Các bước
Tạo list số lượng học sinh.
Tạo list điểm số đạt được.
Để biểu diễn màu cho mỗi điểm phân tán, có list màu.
Dùng Pandas tạo list đại diện cho trục dataframe.
Tạo fig, ax bằng subplots(), mặc định nrows=ncols=1.
Đặt nhãn "Students count" bằng plt.xlabel().
Đặt nhãn "Obtained marks" bằng plt.ylabel().
Tạo điểm phân tán từ dataframe: students_count, marks, color.
Hiển thị figure bằng plt.show().
Ví dụ
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()
Kết quả
