Để tránh chồng chéo thanh lỗi trong matplotlib, thực hiện các bước sau:
Các bước
- Thiết lập kích thước hình và padding.
- Tạo danh sách tên.
- Lấy điểm dữ liệu y1, y2 và lỗi ye1, ye2.
- Tạo figure và subplots.
- Tạo biến đổi affine 2D mutable trans1 và trans2 để dịch chuyển.
- Vẽ errorbar với transform tương ứng.
- Hiển thị hình bằng show().
Ví dụ
import numpy as np import matplotlib.pyplot as plt from matplotlib.transforms import Affine2D plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = ['Jack', 'James', 'Tom', 'Garry'] y1, y2 = np.random.randn(2, len(x)) ye1, ye2 = np.random.rand(2, len(x))*4+0.3 fig, ax = plt.subplots() trans1 = Affine2D().translate(-0.1, 0.0) + ax.transData trans2 = Affine2D().translate(0.1, 0.0) + ax.transData er1 = ax.errorbar(x, y1, yerr=ye1, marker="*", linestyle="none", transform=trans1) er2 = ax.errorbar(x, y2, yerr=ye2, marker="o", linestyle="none", transform=trans2) plt.show()
Kết quả
Hai tập errorbar dịch chuyển ngang nhẹ trái/phải để không chồng lấn nhau.