Python:在2种以上的颜色之间进行插值

Python: interpolate between more than 2 colors

在 R 中,colorRampPalette() 函数非常方便地在 n 颜色之间进行插值以创建新的调色板。我想知道 Python 包中是否有类似的功能? 最佳

# The R code that I would like to translate
pal = colorRampPalette(c("blue", "yellow", "red"))(10)
plot(1:10, 1:10, col=pal, cex=10, pch=16)

好吧,我不知道有任何直接的方法可以达到完全相同的结果,但是由于没有其他回应,让我也许可以指出如果您来自 r,python 通常是如何完成的.

选择颜色范围的一种方法是选择 matplotlib 包的预制颜色图。您可以在此处查看大部分可用的彩色地图:https://matplotlib.org/users/colormaps.html

另一个很好的函数是彩虹函数,它可以根据你想要的颜色数量创建颜色光谱,它的用法如下:

import matplotlib.cm as cm
NrCol = #Enter formula or methode to calculate total number of colours needed 
colour = cm.rainbow(np.linspace(0,1,NrCol))
for j in range(NrCol):
     color = colour[j] #this is the specific colour that you would never stand alone like this but rather use in a plot function

在你得到的所有颜色中,你当然可以选择一定范围,但这将是一个非常粗糙的硬编码解决方案。