代码之家  ›  专栏  ›  技术社区  ›  dputhier

python:在两种以上颜色之间插值

  •  0
  • dputhier  · 技术社区  · 6 年前

    在r,the 颜色渐变调色板() 函数很容易在 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)
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Blue    6 年前

    好吧,我不知道有什么直接的方法可以得到完全相同的结果,但是由于没有其他的响应,让我指出如果您来自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
    

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