Giới thiệu
Để xoay một axes đơn giản trong matplotlib, bạn có thể sử dụng Affine2D và floating_axes từ toolkit axisartist. Các bước:
- Import các package cần thiết:
matplotlib.pyplot,Affine2D,mpl_toolkits.axisartist.floating_axes. - Thiết lập kích thước hình vẽ và padding.
- Tạo figure mới.
- Tạo tuple giới hạn trục
scales. - Tạo phép biến đổi affine 2D
t, thêm phép xoay (độ) vào transform. - Tạo helper lưới cong
GridHelperCurveLinear. - Tạo floating axes
hvà thêm vào figure. - Hiển thị hình vẽ bằng show().
Ví dụ minh họa
# import các package import matplotlib.pyplot as plt from matplotlib.transforms import Affine2D import mpl_toolkits.axisartist.floating_axes as floating_axes # thiết lập kích thước hình plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # vẽ figure fig = plt.figure() scales = (0, 5, 0, 5) # Thêm phép biến đổi affine 2D t = Affine2D().rotate_deg(25) # Thêm floating axes h = floating_axes.GridHelperCurveLinear(t, scales) ax = floating_axes.FloatingSubplot(fig, 111, grid_helper=h) fig.add_subplot(ax) plt.show()
Kết quả đầu ra
Hệ trục tọa độ bị xoay 25 độ:
