Để làm màu đường thay đổi theo chỉ số dữ liệu cho line graph trong matplotlib, thực hiện các bước sau:
Các bước
- Thiết lập kích thước hình và padding.
- Tạo x, y bằng numpy.
- Tính dydx (đạo hàm) làm giới hạn nhỏ hơn.
- Tạo points và segments từ x, y.
- Tạo figure và subplots.
- Tạo class chuẩn hóa dữ liệu tuyến tính.
- Set mảng hình ảnh từ numpy array.
- Đặt linewidth cho collection.
- Set colorbar cho axis 1.
- Tạo Colormap từ danh sách màu r, g, b.
- Lặp lại các bước 6-10.
- Đặt giới hạn trục X, Y.
- Hiển thị hình bằng show().
Ví dụ
import numpy as np import matplotlib.pyplot as plt from matplotlib.collections import LineCollection from matplotlib.colors import ListedColormap, BoundaryNorm plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(0, 3 * np.pi, 500) y = np.sin(x) dydx = np.cos(0.5 * (x[:-1] + x[1:])) points = np.array([x, y]).T.reshape(-1, 1, 2) segments = np.concatenate([points[:-1], points[1:]], axis=1) fig, axs = plt.subplots(2, 1, sharex=True, sharey=True) norm = plt.Normalize(dydx.min(), dydx.max()) lc = LineCollection(segments, cmap='viridis', norm=norm) lc.set_array(dydx) lc.set_linewidth(2) line = axs[0].add_collection(lc) fig.colorbar(line, ax=axs[0]) cmap = ListedColormap(['r', 'g', 'b']) norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N) lc = LineCollection(segments, cmap=cmap, norm=norm) lc.set_array(dydx) lc.set_linewidth(2) line = axs[1].add_collection(lc) fig.colorbar(line, ax=axs[1]) axs[0].set_xlim(x.min(), x.max()) axs[0].set_ylim(-1.1, 1.1) plt.show()
Kết quả
Hai subplot: trên dùng colormap viridis liên tục theo độ dốc, dưới dùng colormap rời rạc r/g/b theo ngưỡng độ dốc.