1
    d = {"Fruits" : ["Apples","Bananas","Pears","Oranges","Melons"], 
     "Ripe" : [10000,18000,19000,36000,50000],
     "Rotten" : [3000,8000,7000,10000,20000] 
    }
df = pd.DataFrame(d)

df["Difference"]= df["Ripe"] - df["Rotten"]
df

I am working on this small dataframe and cant seem to apply custom colors on the function i created to generate vbars

source=ColumnDataSource(data=dict(fr=df.Fruits,
                                 ri=df.Ripe,
                                 ro=df.Rotten,
                                 di=df.Difference))
tooltips=[('Total','@ri')]

reds=['#f14331',
  '#f14432',
  '#f24633',
  '#f24734',
  '#f34935']

def vbar(source,x_range,x,top,color=reds):
    p=figure(toolbar_location=None,height=300,x_range=x_range,sizing_mode='stretch_width')
    p.xgrid.grid_line_color=None
    p.y_range.start=0
    
    p.vbar(x=x,top=top,source=source,width=0.8,color=color)
    
    p.add_tools(HoverTool(tooltips=tooltips))
    
    return p

p=vbar(source,x_range=df.Fruits,x='fr',top='ri')
show(p)

in the function parameter when i pass in a color such as'blue' it runs totally fine. but when i pass in a list of colors i.e reds, it throws an error:

    ---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Input In [151], in <cell line: 24>()
     20     p.add_tools(HoverTool(tooltips=tooltips))
     22     return p
---> 24 p=vbar(source,x_range=df.Fruits,x='fr',top='ri')
     25 show(p)

Input In [151], in vbar(source, x_range, x, top, color)
     15 p.xgrid.grid_line_color=None
     16 p.y_range.start=0
---> 18 p.vbar(x=x,top=top,source=source,width=0.8,color=color)
     20 p.add_tools(HoverTool(tooltips=tooltips))
     22 return p

File ~\Anaconda3\lib\site-packages\bokeh\plotting\_decorators.py:86, in glyph_method.<locals>.decorator.<locals>.wrapped(self, *args, **kwargs)
     84 if self.coordinates is not None:
     85     kwargs.setdefault("coordinates", self.coordinates)
---> 86 return create_renderer(glyphclass, self.plot, **kwargs)

File ~\Anaconda3\lib\site-packages\bokeh\plotting\_renderer.py:96, in create_renderer(glyphclass, plot, **kwargs)
     94 incompatible_literal_spec_values += _process_sequence_literals(glyphclass, glyph_visuals, source, is_user_source)
     95 if incompatible_literal_spec_values:
---> 96     raise RuntimeError(_GLYPH_SOURCE_MSG % nice_join(incompatible_literal_spec_values, conjuction="and"))
     98 # handle the nonselection glyph, we always set one
     99 nonselection_visuals = pop_visuals(glyphclass, kwargs, prefix='nonselection_', defaults=glyph_visuals, override_defaults={'alpha':0.1})

RuntimeError: 

Expected line_color, fill_color and hatch_color to reference fields in the supplied data source.

When a 'source' argument is passed to a glyph method, values that are sequences
(like lists or arrays) must come from references to data columns in the source.

For instance, as an example:

    source = ColumnDataSource(data=dict(x=a_list, y=an_array))

    p.circle(x='x', y='y', source=source, ...) # pass column names and a source

Alternatively, *all* data sequences may be provided as literals as long as a
source is *not* provided:

    p.circle(x=a_list, y=an_array, ...)  # pass actual sequences and no source

i have also tried color='reds' with quotation marks

which gives a blank graph and spits out:

ERROR:bokeh.core.validation.check:E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name. This could either be due to a misspelling or typo, or due to an expected column being missing. : key "fill_color" value "reds", key "hatch_color" value "reds", key "line_color" value "reds" [renderer: GlyphRenderer(id='22999', ...)]

any advice on how i can get my graph to run with my custom colors?

1 Answers1

0

the issue was I had to specify the colors within the CDS

source=ColumnDataSource(data=dict(fr=df.Fruits,
                                 ri=df.Ripe,
                                 ro=df.Rotten,
                                 di=df.Difference,
                                 color=reds))
tooltips=[('Total','@ri')]

reds=['#e83b1e','#ea4e34','#ec624a','#ee7561','#f18978']

def vbar(source,x_range,x,top,color='color'):
    p=figure(toolbar_location=None,height=300,x_range=x_range,sizing_mode='stretch_width')
    p.xgrid.grid_line_color=None
    p.y_range.start=0
    
    p.vbar(x=x,top=top,source=source,width=0.8,color=color)
    
    p.add_tools(HoverTool(tooltips=tooltips))
    
    return p

p=vbar(source,x_range=df.Fruits,x='fr',top='ri')
show(p)