簡體   English   中英

為字典中的數字設置顏色比例[Python]

[英]Make color scale for numbers in dictionary [Python]

我有一本具有不同值的字典,如下所示:

  colormap_coms=
  {0.10208554259455638: rgb(179, 56, 79), 0.0: rgb(121, 28, 149), 
  0.10870219813309245: rgb(44, 147, 200), 0.12623481073520415: rgb(78, 170, 
  98), 0.10298102981029811: rgb(150, 87, 91), 0.044263775971093045: rgb(223, 
  39, 33), 0.13340724316334074: rgb(194, 205, 92), 0.10034049058439304: 
  rgb(99, 41, 106), 0.08040984519434236: rgb(97, 51, 26), 
  0.008130081300813009: rgb(106, 148, 70), 0.07158437438032918: rgb(54, 159, 
  37)}

我使用了這個功能:

 colormap_coms = {x : 
 RGB(random.randrange(0,256),random.randrange(0,256),random.randrange(0,256)) 
                 for x in list(set(communities))}

在這里,我有隨機的顏色分配,但是我想基於值(從較小到較大)分配顏色比例(例如,從暗到亮)。

有沒有簡單的方法可以做到這一點? 謝謝

RGB不適合創建平滑的顏色過渡,即Google HSV顏色空間。 你選擇一個^ h UE,A S aturation,使值變量來創建一個基地深淺不同的顏色。

您可以從HSV值中獲取RGB值,如下所示:

 import colorsys rgbs = [] for v in range(10000): val = v/10000.0 # http://colorizer.org/ some light blue base color rgbs.append( colorsys.hsv_to_rgb(170/256.0,0.7,val)) print (rgbs) 

(從CoryKramer的答案此處修改)

您可以位RGB此列表映射到你的價值觀,或者干脆把你的值到HSV的值-你可能需要在你的原始值一些縮放比例/正規化超過可用的色彩空間傳播他們。


在這種情況下,您還可以修復其他2個:H或V,此時色相或值會發生變化。

創建與rbg相似的映射,並且應設置為:

coms= sorted([0.10208554259455638, 0.0, 0.10870219813309245,0.12623481073520415, 0.10298102981029811,0.44263775971093045,0.13340724316334074,0.10034049058439304,0.08040984519434236,0.008130081300813009,0.07158437438032918])

mycols = {}
part = 1.0 / len(coms)
for k in range(len(coms)): 
    mycols[colormap_coms[k]] = colorsys.hsv_to_rgb(170/256.0,0.7,k*part)

print(mycols)

輸出:

    {0.0: (0.0, 0.0, 0.0), 
     0.008130081300813009: (0.02727272727272728, 0.028267045454545465, 0.09090909090909091), 
     0.07158437438032918: (0.05454545454545456, 0.05653409090909093, 0.18181818181818182), 
     0.08040984519434236: (0.08181818181818182, 0.08480113636363638, 0.2727272727272727), 
     0.10034049058439304: (0.10909090909090911, 0.11306818181818186, 0.36363636363636365),
     0.10208554259455638: (0.1363636363636364, 0.14133522727272732, 0.4545454545454546), 
     0.10298102981029811: (0.16363636363636364, 0.16960227272727277, 0.5454545454545454), 
     0.10870219813309245: (0.19090909090909094, 0.19786931818181824, 0.6363636363636364), 
     0.12623481073520415: (0.21818181818181823, 0.22613636363636372, 0.7272727272727273), 
     0.13340724316334074: (0.2454545454545455, 0.2544034090909092, 0.8181818181818182), 
     0.44263775971093045: (0.2727272727272728, 0.28267045454545464, 0.9090909090909092)}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM