3

I am not sure this is a functionality in plotnine, but I want to specifically assign colors to values.

For example if I'm creating a scatter plot

colors = {True:'#a54f7e', False:'grey'}  

(ggplot(df, aes(x = "col1", y="col2", color = "col3")) +
     geom_jitter(size = 3, color = colors)

So in this situation, col3 would be True/False values where ideally I'd want all True values to be #a54f7e and all False values to be grey. The code above does not run with this error:

PlotnineError: "'{True: 'red', False: 'blue'}' does not look like a valid value for `color`"

Help!

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
mk2080
  • 872
  • 1
  • 8
  • 21

1 Answers1

2

If you want to assign colors make use of scale_color_manual:

import pandas as pd
from plotnine import *

data = [[1, 4.5,True], [2, 4.25,False], [3, 3.75,False],[4, 3.5,True],[1, 4.0,False],[2, 3.75,False],[3, 4.0,False],[4, 4.25,True]]

df = pd.DataFrame(data, columns = ['col1', 'col2','col3'])

colors = {True:'#a54f7e', False:'grey'}  

(ggplot(df, aes(x = "col1", y="col2", color = "col3")) +
     geom_jitter(size = 3) +
     scale_color_manual(values = colors))

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51