如何给每个类别一个颜色?

时间:2016-06-13 23:04:39

标签: python json if-statement colors

我们有一个代码可以在地图上的位置上绘制圆圈,并使用每个类别的名称。现在圆圈和文字都是一种颜色。我们如何按类别以不同的颜色获取它们?示例:类别花园:蓝色,类别石材:灰色。

到目前为止代码:

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

1 个答案:

答案 0 :(得分: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

这看起来过分,甚至还没有经过轻微测试(只是一个ste)但我今晚将测试并合并此代码:: http://krewn.github.io/KPlot/