Changing circles colours

Hi guys, I’m trying to change colours of circles into my chart. I wanna make a different colours for every of chosen cars, code below.

from bokeh.io import curdoc
curdoc().theme = “dark_minimal”

data=autompg[autompg.make.isin(‘ford volkswagen honda’.split())]

x = data.yr
y = data.mpg

p = figure(
title=“mpg / yr”,
y_range=(0, 55),
sizing_mode=“stretch_width”,
max_width=500,
height=350,
)

p.circle(x, y, size=15, fill_alpha=0.5)

p.xaxis.axis_label = “yr”
p.xaxis.axis_line_width = 3
p.xaxis.axis_line_color = “red”

p.yaxis.axis_label = “mpg”
p.yaxis.major_label_text_color = “red”
p.yaxis.major_label_orientation = “vertical”

p.axis.minor_tick_in = -3
p.axis.minor_tick_out = 6

show(p)

Thank you in advance.

Hi @Oskar ! Welcome to the Bokeh Discourse.

You may want to use a factor map. I reworked your code a little bit and based a factor_cmap on the example in the User Guide: Handling Categorical Data.

from bokeh.io import show
from bokeh.plotting import figure
from bokeh.sampledata.autompg import autompg_clean as autompg
from bokeh.palettes import Spectral3
from bokeh.transform import factor_cmap
from bokeh.models import ColumnDataSource

data = autompg.loc[autompg['mfr'].isin('ford volkswagen honda'.split())]

x = data.yr
y = data.mpg
m = data.mfr
mfr_map = factor_cmap('m', palette=Spectral3, factors=list(set(m)))
p = figure(title="mpg / yr",
           y_range=(0, 55),
           sizing_mode="stretch_width",
           max_width=500,
           height=350
           )

cds = ColumnDataSource(data=dict(x=data.yr, y=data.mpg, m=data.mfr))

p.circle(x='x', y='y', size=15, fill_alpha=0.5, color=mfr_map, source=cds)

p.xaxis.axis_label = "yr"
p.xaxis.axis_line_width = 3
p.xaxis.axis_line_color = "red"

p.yaxis.axis_label = "mpg"
p.yaxis.major_label_text_color = "red"
p.yaxis.major_label_orientation = "vertical"

p.axis.minor_tick_in = -3
p.axis.minor_tick_out = 6

show(p)

I chose Spectral3, but you have several ready-made palettes to choose from, or you could define a list of colors yourself to map to the different auto manufacturers.

Thanks for posting, and let me know if you have questions! One quick note, though-- for any code samples pasted in future, do please make sure they’re runnable as-is (this one was not, and someone copying and pasting the code would have had to add imports to get it going).

2 Likes

Thank you very much !

1 Like

I’ve got another question, if I may. Trying to make a label for these three cars brands with this documentations

but always something is wrong …

Can you provide a screenshot of what’s going wrong? Is it Labels you want, or a Legend?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.