Mine
Mine

Reputation: 861

Color ggplot2 values by a sorted unique column values in Python

I came across this package plotnine which can give the same results as R's ggplot2 in Python. It's pretty useful but I have a problem coloring "label_1" and "label_2" values by their unique "ID"s. The colors should be distinguishable. It could be ranging from a bright shade of a color to the darkest shade. My code gives a pretty close result to what I wish but still the colors are not distinguishable enough. My graph is not using my colors now but I'd like to find out if it could work out too.

# Generating 100 random colors for 100 values
from plotnine import *

import random as random
colors= lambda n: list(map(lambda i: "#" + "%06x" % random.randint(0, 0xFFFFFF), range(n)))
colors = colors(100)


ggplot(
    musk_df) + geom_point(aes(x = 'label_1',y = 'label_2',fill = 'id'),alpha = 0.5) +labs(
        title ='Graph',
        x = 'label_1',
        y = 'label_2',) +scale_fill_manual(
        name = 'id',values = colors) +scale_fill_gradient(low="green",high="darkgreen") 

Upvotes: 0

Views: 455

Answers (1)

Cowflu
Cowflu

Reputation: 332

When using scale_fill_manual, you have to create a dict that associates the fill element and a color. For istance:

color_dict = {'ID1': 'green', 
              'ID2': 'red',...
              ...
              'IDN': 'darkgreen'}

You have to modify the key of the dictionary with the unique values of your id column. That should do the trick.

Upvotes: 2

Related Questions