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

Đảo ngược colormap của hình ảnh trong Matplotlib


Các bước

  1. Thiết lập kích thước hình và padding.
  2. Tạo dữ liệu ngẫu nhiên x, y.
  3. Lấy colormap 'Blues' bằng get_cmap().
  4. Thêm subplot tại vị trí 1.
  5. Vẽ scatter với colormap gốc.
  6. Tạo colorbar.
  7. Vẽ scatter với colormap đảo ngược (reversed()).
  8. Đặt tiêu đề cho cả hai trục.
  9. Gọi show().

Ví dụ

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

x = np.linspace(-2, 2, 10)
y = np.linspace(-2, 2, 10)
color_map = plt.cm.get_cmap('Blues')

plt.subplot(1, 2, 1)
sc = plt.scatter(x, y, c=x, cmap=color_map)
plt.colorbar(sc)
plt.title("Colorbar")

plt.subplot(1, 2, 2)
sc = plt.scatter(x, y, c=x, cmap=color_map.reversed())
plt.colorbar(sc)
plt.title("Reversed Colorbar")
plt.show()

Kết quả

Đảo ngược colormap của hình ảnh trong Matplotlib