Để thiết lập rcParams cục bộ cho một figure trong matplotlib, sử dụng context manager plt.rc_context().
Các bước
- Thiết lập kích thước hình và padding.
- Khởi tạo biến N số mẫu.
- Tạo x, y bằng numpy.
- Dùng
plt.rc_context()để thay đổi tạm thời rcParams. - Thêm subplot 1, vẽ x, y.
- Thêm subplot 2, vẽ x, y.
- Hiển thị hình bằng show().
Ví dụ
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
N = 10
x = np.random.rand(N)
y = np.random.rand(N)
with plt.rc_context({"axes.grid": True, "grid.linewidth": 0.75, "lines.linestyle": 'dashed'}):
plt.subplot(121)
plt.plot(x, y)
plt.subplot(122)
plt.plot(x, y)
plt.show()
Kết quả
Subplot bên trái có lưới, đường nét đứt; subplot bên phải dùng rcParams mặc định (không lưới, đường liền).