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()
1条答案
按热度按时间fjnneemd1#