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

Thay đổi màu nền mặc định cho biểu đồ Matplotlib

Để thay đổi màu nền mặc định cho biểu đồ Matplotlib:

Các bước

  • Thiết lập kích thước hình và điều chỉnh khoảng đệm subplot.
  • Lấy axes hiện tại.
  • Thêm subplot với nrows=1, ncols=2, index=1.
  • Vẽ điểm ngẫu nhiên x, y bằng plot().
  • Đặt tiêu đề subplot.
  • Thêm subplot thứ 2 với nrows=1, ncols=2, index=2.
  • Lấy axes hiện tại.
  • Đặt facecolor tùy chỉnh.
  • Vẽ x, y bằng plot().
  • Đặt tiêu đề.
  • Hiển thị hình 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
ax = plt.gca()
print("Default face color is: ", ax.get_facecolor())
plt.subplot(121)
plt.plot(np.random.rand(10), np.random.rand(10))
plt.title("With default face color")
plt.subplot(122)
ax = plt.gca()
ax.set_facecolor("orange")
plt.plot(np.random.rand(10), np.random.rand(10))
plt.title("With customize face color")
plt.show()

Kết quả

Thay đổi màu nền mặc định cho biểu đồ Matplotlib