matplotlib 颜色点根据其位置

new9mtju  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(154)

我想绘制一个变分自动编码器模型的特征向量。潜在向量由八个单独的值组成。由于每个维度都代表一个属性,因此不可能在2d平面中绘制它们。因此,我决定用8x8的子图来显示两个潜在维度对之间的关系,其中x轴和y轴分别代表一个属性,每个图由20个点组成。是否有可能仅根据点的x和y分量来着色点,以突出它们与所述属性(维度)的连接?我愿意听取你关于更好的说明的任何建议。我很感激任何帮助。

fjnneemd

fjnneemd1#

import matplotlib.pyplot as plt
import numpy as np

# I think it only works with the combination of two of these colors: 
# red, green and blue.
# ----------------------------
# red: [1.0, 0.0, 0.0]
# green: [0.0, 1.0, 0.0]
# blue: [0.0, 0.0, 1.0]
# For example: if you use the color orange, you can have 
# different points with the same color.
base_x_color = np.array([0.0, 1.0, 0.0])
base_y_color = np.array([1.0, 0.0, 0.0])

# by all plots, dummy values to max and min
x_min = 0.
x_max = 10.
y_min = 1.0
y_max = 15.
range_x = x_max - x_min
range_y = y_max - y_min

# an any plot, dummy values to x and y
x = np.array([0.5, 5., 6., 3., 8., 9., 0.7, 3.6])
y = np.array([1.2, 3., 11., 3.5, 1.7, 12.3, 14.8, 2.5])
x_scale = (x - x_min)/range_x
y_scale = (y - y_min)/range_y
cx = x_scale[:, np.newaxis]*base_x_color
cy = y_scale[:, np.newaxis]*base_y_color
cxy = cx + cy

# all combinations
n_colors = 16 # like a resolution
xp = np.linspace(0, 1, n_colors)
yp = np.linspace(0, 1, n_colors)
Xp, Yp = np.meshgrid(xp, yp)
x_colors = Xp[:, :, np.newaxis]*base_x_color
y_colors = Yp[:, :, np.newaxis]*base_y_color
colors = x_colors + y_colors

# comparision
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.scatter(x, y, c=cxy)
ax2.imshow(colors)

ax1.set_xlim(x_min, x_max)
ax1.set_ylim(y_min, y_max)
ax1.set_aspect(0.7)

ax2.set_aspect('equal')
ax2.invert_yaxis()

相关问题