Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Vẽ yscale: linear, log, symlog, logit trong Matplotlib

Matplotlib hỗ trợ 4 loại scale trục Y: linear, log, symlog (symmetric log), logit. Chuyển đổi bằng plt.yscale().

Các bước

  • Thiết lập figure size và autolayout.
  • Tạo dữ liệu y phân phối chuẩn (0,1), sắp xếp.
  • Tạo x =.arange(len(y)).
  • Tạo 4 subplot 2x2.
  • Mỗi subplot: plot(x,y), set yscale('linear'/'log'/'symlog'/'logit'), set title.
  • Hiển thị bằng show().

Ví dụ

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))

# linear
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')

# log
plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')

# symmetric log
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthreshy=0.01)
plt.title('symlog')

# logit
plt.subplot(224)
plt.plot(x, y)
plt.yscale('logit')
plt.title('logit')

plt.show()

Kết quả

Vẽ yscale: linear, log, symlog, logit trong Matplotlib