Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

python 3.x - Random colors for a tree map with squarify / RGBA values should be within 0-1 range

I'm creating a few maps with squarify and I'm trying to create a list of random colors with length = len(df). I've seen this similar thread, but don't see the solution there.

for row in range(len(df)):
    random_number = random.randint(0, 16777215)
    hex_number = str(hex(random_number))
    hex_number = '#' + hex_number[2:]
    colors.append(hex_number)
# Plotting
squarify.plot(sizes=df, label=df.index, color=colors)

With the code above, I ended up with Invalid RGBA argument.

So I've tried RBG colors based on this thread/answer:

for row in range(len(df)):
    color = list(np.random.choice(range(256), size=3))
    color.append(0.7) #opacity
    colors.append(color)
# Plotting
squarify.plot(sizes=df, label=df.index, color=colors)

But then I get a RGBA values should be within 0-1 range, but this is not what I'd expect of RGBA values.

Any tip on how I could create generate these random RGBA values for squarify? Thanks!

question from:https://stackoverflow.com/questions/65865072/random-colors-for-a-tree-map-with-squarify-rgba-values-should-be-within-0-1-ra

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...