Có thể dùng ax.loglog(x, y) và phương thức set_major_formatter() để thay thế tick labels bằng giá trị tính toán.
Các bước
- Thiết lập kích thước figure và padding
- Tạo figure và subplots
- Vẽ đồ thị log-log trên cả 2 trục
- Đặt formatter cho major ticker
- Hiển thị bằng
show()
Ví dụ mã nguồn
import numpy as np from matplotlib import pyplot as plt, ticker plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() ax.loglog(np.logspace(0, 5), np.logspace(0, 5)**2) ax.xaxis.set_major_formatter(ticker.LogFormatterExponent()) plt.show()
Kết quả
Đồ thị log-log với trục X hiển thị dạng số mũ (10^0, 10^1, 10^2...) thay vì giá trị đầy đủ.

Các formatter hữu ích:
LogFormatterExponent()- hiển thị 10^nLogFormatterMathtext()- hiển thị $10^n$FuncFormatter(lambda x, pos: f'{x:.0f}')- tự định nghĩaStrMethodFormatter('{x:.1f}')- format string