Trong chương trình này, vẽ hai đường line bằng matplotlib. Trước khi code, import matplotlib.pyplot as plt. Pyplot là tập hợp hàm lệnh style làm matplotlib hoạt động như MATLAB.
Thuật toán
Bước 1: Import matplotlib.pyplot Bước 2: Định nghĩa điểm line1 và line2. Bước 3: Vẽ line bằng plot() trong pyplot. Bước 4: Định nghĩa title, X-axis, Y-axis. Bước 5: Hiển thị bằng show().
Mã ví dụ
import matplotlib.pyplot as plt
line1_x = [10, 20, 30]
line1_y = [20, 40, 10]
line2_x = [10, 20, 30]
line2_y = [40, 10, 30]
plt.xlabel('X AXIS')
plt.ylabel('Y AXIS')
plt.plot(line1_x, line1_y, color='blue', linewidth=3, label='line1-dotted', linestyle='dotted')
plt.plot(line2_x, line2_y, color='red', linewidth=5, label='line2-dashed', linestyle='dotted')
plt.title("PLOTTING DOTTED LINES")
plt.legend()
plt.show()
Kết quả

Giải thích
Biến line1_x, line1_y và line2_x, line2_y là tọa độ line. linewidth là độ dày line. plt.legend() đặt chú thích tên trục trên đồ thị.