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

Cách thay đổi figsize cho matshow() trong Jupyter notebook dùng Matplotlib

Sử dụng plt.figure(figsize=...) và tham số fignum trong matshow().

  • plt.figure() tạo figure mới.
  • Tạo DataFrame pandas.
  • plt.matshow(df.corr(), fignum=1) vẽ ma trận tương quan vào figure 1.
  • Hiển thị bằng show().

Ví dụ

import pandas as pd
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
plt.figure()
df = pd.DataFrame({"col1": [1, 3, 5, 7, 1], "col2": [1, 5, 7, 9, 1]})
plt.matshow(df.corr(), fignum=1)
plt.show()

Kết quả

Ma trận tương quan (heatmap) hiển thị với kích thước figure tùy chỉnh.