这是一次性完成所有更换的解决方案:
import matplotlib.colors as clr
import matplotlib.pyplot as plt
import numpy as np
N = 100000
x = 1.2 + 800.0 * np.random.rand(N)
y = 1.2 + 800.0 * np.random.rand(N)
# Generate random colors of the form (r, g, b, a) where r = 0.0
colors = np.random.rand(4 * N).reshape((N, 4))
colors[:, 0] = 0.0
area = np.pi * (5 * np.random.rand(N))**2
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
pcol = ax.scatter(x, y, s=area, c=colors)
ax.set_xscale('log')
ax.set_yscale('log')
# Save initial figure
plt.savefig("hue.jpg")
oldRGBA = pcol.get_facecolors().reshape((N, 1, 4))
oldRGB = oldRGBA[:, :, :3]
newRGB = oldRGB
newRGB[:, :, 0] = 1.0 # Set red component to 1.0
oldHSV = clr.rgb_to_hsv(oldRGB)
newHSV = clr.rgb_to_hsv(newRGB)
oldHSV[:, :, 0] = newHSV[:, :, 0]
newRGBA = np.copy(oldRGBA)
newRGBA[:, :, :3] = clr.rgb_to_hsv(oldHSV)
pcol.set_facecolors(newRGBA[:, 0, :])
# Save modified figure
plt.savefig("hue_bis.jpg")
plt.close()
正如您所看到的,此代码尝试绘制 100000 个点,实际上它在大约 2 秒内完成了这一任务。以下是产生的数字:
and :
关于你的最后两个问题:
如何将给定 RGBA 颜色 A 的色调更改为给定 RGB 颜色 B 的色调,同时保留 A 的 alpha 值?
and :
使用不同的颜色模型(例如 HSL)会简化任务吗?如果是这样,这将有所帮助
我认为你进行这种修改的方法是值得赞赏的,它避免了手工计算(参见HSL 和 HSV http://en.wikipedia.org/wiki/HSL_and_HSV)。使用不同的颜色模型是可能的,HSL 和 HSV 都允许更改色调而不影响其他参数,但这只是另一种方法,而不是更好的方法。
希望这会有所帮助。