2016-06-13 8 views
0

地図上の場所に各カテゴリの名前で円を描くコードがあります。今、円とテキストは1つの色です。私たちはどのようにカテゴリー別に色を変えますか?例:カテゴリガーデン:青、カテゴリストーン:グレー。各カテゴリに色を付けるにはどうすればよいですか?

これまでコード:

size(1500,800) 
background(1) 
nofill() 
stroke('#f91') 
pen(.2) 
fill('#f91', 0.05) 
rotate(90) 

font("Avenir", "bold", 10) 
align('left') 


def mapValue(value, fromMin, fromMax, toMin, toMax): 
    # Figure out how 'wide' each range is 
    fromSpan = fromMax - fromMin 
    toSpan = toMax - toMin 

    # Convert the from range into a 0-1 range (float) 
    valueScaled = float(value - fromMin)/float(fromSpan) 

    # Convert the 0-1 range into a value in the to range. 
    return toMin + (valueScaled * toSpan) 

def xOfDot(lon): 
    return mapValue(lon, -100, 100, 0, WIDTH) 

def yOfDot(lat): 
    return mapValue(lat, -90, 90, HEIGHT, 0) 


with open('theft-alerts.json', 'r') as inputFile: 
    data = json.load(inputFile) 

print len(data) 

artworksPerCity = {} 

for stolenArt in data: 
    if stolenArt.has_key('Category'): 
     city = stolenArt['Category'] 
     if stolenArt.has_key('nItemsStolen'): 
      numbersStolen = int(stolenArt['nItemsStolen']) 
      if artworksPerCity.has_key(city): 
       # Adjust the value stored for this city 
       artworksPerCity[city] = artworksPerCity[city] + numbersStolen 
      else: 
       # Create new key with new value 
       artworksPerCity[city] = numbersStolen 
      # Draw circle on the map 
      radius = artworksPerCity[city] /2 
      x = xOfDot(stolenArt['Lon']) 
      y = yOfDot(stolenArt['Lat']) 
      arc(x, y, radius) 
      text(city, x, y) 

print artworksPerCity 
+0

特定の回答について、あなたは使用しているグラフィックスライブラリを説明する必要があります。この質問にはかなり無駄なタグがたくさんあります(たとえば、「json」は見られません)、関連するものと置き換えてください! – Blckknght

答えて

0

ここでは、私は私の純粋なPythonのデータユーティリティに含める予定のもののスケッチです。

def hexidecimalDiget(n,deHex = false): 
    if(n<0): 
     print "negitive values not supported by call to hexidecimalDiget("+str(n)+")" 
     return None 
    elif(n < 10): 
     return str(n) 
    elif(n < 15): 
     return ["a","b","c","d","e"][n-10] 
    elif(n in ["a","b","c","d","e"]): 
     if deHex: 
      return ["a","b","c","d","e"].index(n) 
     return n 
    else: 
     print "call to hexidecimalDiget("+str(n)+") not supported!" 
     return None 

def colorFormHexArray(arr): 
    if len(arr)!=3 and len(arr)!=6: 
     print "invalid length for color on call to colorFormHexArray("+str(arr)+")" 
     return None 
    elif None in arr: 
     print "cannot make color from None arguments in "+str(arr) 
     return None 
    else: 
     ret = "#" 
     for k in arr: 
      if(type(k) == list): 
       for k2 in k: 
        ret+=hexidecimalDiget(k) 
      else: 
      ret+=hexidecimalDiget(k) 
     return ret 

def arrayFromColor(c): 
    c = c.replace("#","") 
    col = [] 
    for n,k in enumerate(c): 
     if(len(c) == 3): 
      col.append([hexidecimalDiget(k,deHex = True)]) 
     elif(len(c) == 6): 
      col.append([hexidecimalDiget(c[(n+1)*2-2],deHex = True),hexidecimalDiget(c[(n+1)*2-2],deHex = True)]) 
    return(col) 

def intFromHexPair(hp): 
    ret = 0 
    for n,k in enumerate(hp): 
     digBase = 16**(len(hp)-n-1) 
     ret+=digBase*hexidecimalDiget(hp[0],deHex = True) 
    return ret 

def hexPairFromInt(I,minDigits = 1,maxDigits = 256): 
    if I<0: 
     print "negitive numbers not supported by hexPairFromInt" 
    k= 0 
    while(16**(k+1) <= I): 
     k+=1 
    if k < minDigits: 
     k = minDigits 
    if k > minDigits: 
     print("maxDigitsExceeded") 
    ret = [] 
    while k>=0 
     dig = 16**k 
     ret.append(hexidecimalDiget(int(I)%(dig)) 
     I -= dig 
     k-=1 
    return ret 

def specColor(start,end,bottom,top): 
    start = arrayFromColor(start) 
    end = arrayFromColor(end) 
    def ret(v): 
     if(v<start or c>end): 
      print("value out of range "+str([start,end])) 
      return('#aa0000') #eyo <- error red 
     else: 
      starts = [intFromHexPair(k) for k in start] 
      ends = [intFromHexPair(hp) for k in end] 
      normalized = (v-bottom)/(top-bottom) 
      return colorFormHexArray([hexPairFromInt(int((starts[n]-ends[n])*normalized),minDigits = 1,maxDigits = 256) for n,k in enumerate(starts)]) 
    return ret 

これは、過剰なようだし、少しでもまだ(ちょうどstetchアップ気圧)をテストされていないが、私は今夜ここにこのコードをテストして組み込むことになります:: http://krewn.github.io/KPlot/

関連する問題