Các bước
- Thiết lập kích thước hình và padding.
- Tạo dữ liệu
speed, acceleration bằng numpy.
- Thêm subplot vào figure hiện tại.
- Vẽ speed bằng
plot().
- Tạo trục Y thứ hai chia sẻ trục X bằng
twinx().
- Vẽ acceleration trên trục Y thứ hai.
- Đặt legend cho figure.
- Gọi
show().
Ví dụ
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
speed = np.array([3, 1, 2, 0, 5])
acceleration = np.array([6, 5, 7, 1, 5])
ax1 = plt.subplot()
l1, = ax1.plot(speed, color='red')
ax2 = ax1.twinx()
l2, = ax2.plot(acceleration, color='orange')
plt.legend([l1, l2], ["speed", "acceleration"])
plt.show()
Kết quả
