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

Cách thay đổi kích thước font ticks của colorbar trong Matplotlib

Dùng tick_params(labelsize=...) trên axes của colorbar (cbar.ax).

  • Tạo dữ liệu ngẫu nhiên 5x5.
  • im = plt.imshow(...), cbar = plt.colorbar(im).
  • tick_font_size = 10.
  • cbar.ax.tick_params(labelsize=tick_font_size).
  • Hiển thị bằng show().

Ví dụ

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
data = np.random.rand(5, 5)
im = plt.imshow(data, interpolation="nearest", cmap="copper")
cbar = plt.colorbar(im)
tick_font_size = 10
cbar.ax.tick_params(labelsize=tick_font_size)
plt.show()

Kết quả

Colorbar với font chữ ticks kích thước 10.