我正在尝试在正方形的四个角上绘制一个包含四种不同颜色的图,并且在它们被涂抹的颜色之间有明显的分离(白色阴影)。下面是我的代码,它可以绘制但区域之间没有清晰的颜色分离,那里有点黑灰色,如果它接近绿色、黄色或蓝色则很难区分。
from scipy.interpolate import interp2d
import numpy as np
import matplotlib.pyplot as plt
smoothSeparationOfColors = False
# Create a list of the four corner colors
red = (1, 0, 0)
green = (0, 1, 0)
blue = (0, 0, 1)
yellow = (1, .84, 0)
colors = [red, green, blue, yellow]
# Create a 2D grid of x and y coordinates
x = np.array([0, 0, 1, 1])
y = np.array([0, 1, 1, 0])
# Create a 2D grid of the RGB values for each corner
r = np.array([color[0] for color in colors]) # first value from each color tuple
g = np.array([color[1] for color in colors]) # second value from each color tuple
b = np.array([color[2] for color in colors]) # third value from each color tuple
# Create 2D interpolation functions for each color channel
red_interp = interp2d(x, y, r, kind='linear')
green_interp = interp2d(x, y, g, kind='linear')
blue_interp = interp2d(x, y, b, kind='linear')
# Create a grid of x and y coordinates for the image
x_coords, y_coords = np.linspace(0, 1, 100), np.linspace(0, 1, 100)
# Interpolate the color values for each point on the grid
r_interp = red_interp(x_coords, y_coords)
g_interp = green_interp(x_coords, y_coords)
b_interp = blue_interp(x_coords, y_coords)
# Create an image from the interpolated color values
colors_interp = np.dstack((r_interp, g_interp, b_interp))
if smoothSeparationOfColors:
for j in range(len(colors_interp)):
for i in range(len(colors_interp[j])):
rgb_max = max(colors_interp[j][i][0], colors_interp[j][i][1], colors_interp[j][i][2])
if rgb_max > 0:
colors_interp[j][i][0] /= rgb_max
colors_interp[j][i][1] /= rgb_max
colors_interp[j][i][2] /= rgb_max
# Show the interpolated image
plt.imshow(colors_interp, extent=[0, 1, 0, 1])
plt.xlim(-0.5, 1.5)
plt.ylim(-0.5, 1.5)
plt.savefig('fourcolorPlot.svg')
plt.show()
当我
smoothSeparationOfColors
关闭时,这就是情节的样子-
但如果我打开它,那么情节看起来像这样-
但这不是我想要的。我想让那个白点位于正方形的质心处,并且有四条分隔线从左-右-上-下移动。只有 3 个颜色维度的 python 有点难,这就是我挣扎的原因。请帮忙!提前谢谢你。