☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

Map color to scatter plot points from array

Community Support
Kevin
Kevin

Hey everyone, when plotting the cluster assignment/labels output from scikit’s k-means with matplotlib one can do this:

plt.scatter(sqrt(X[:,0]), sqrt(X[:,1]), s = 100, c = k_means.labels_ , alpha=0.4)

plt.autoscale(tight=True)

Matplotlib will chose colors from the k_means.labels array that might look like [0,1,2,0,1,0,0,0,2]

Doing something similar in Bokeh, doesn’t work:

from bokeh.plotting import *

output_notebook()

scatter(X[:,0],X[:,1], c = k_means.labels_ , alpha=0.4,

fill_alpha=0.6, name=“color_scatter_example”, title = “k-means clusters”)

Is there something better than manually mapping the color with an array that explicitly defines colors like:

color_list =

for i in k_means.labels_:

if i == 0:

color_list.append(’#d18096’)

elif i ==1:

color_list.append(’#483496’)

else:

color_list.append(’#00FFD0’)

⌁   Link to post
Peter_Wang1
Peter_Wang1

Hey Kevin,

The bad news is that you do have to manually map the colors, but the good news is that it can be done relatively easily with Numpy. Below is a code sample.

-Peter

from bokeh.plotting import *

from numpy import *

N = 9

x = np.linspace(-2, 2, N)

y = x**2

output_file(“glyphs.html”, title=“glyphs.py example”)

color_list = array((‘#d18096’, ‘#483496’, ‘#00FFD0’))

labels = [0,1,2,0,1,0,0,0,2]

Materialize the full list of colors, using Numpy’s “fancy indexing”

(which is why we needed to create color_list as a numpy array)

colors = color_list[labels]

scatter(x, y, type=“circle_x”, size=16,

fill_color=colors, fill_alpha=0.6, alpha=0.4)

show()

···

On Thu, Jan 2, 2014 at 4:38 PM, Kevin Davenport skycondition@gmail.com wrote:

Hey everyone, when plotting the cluster assignment/labels output from scikit’s k-means with matplotlib one can do this:

plt.scatter(sqrt(X[:,0]), sqrt(X[:,1]), s = 100, c = k_means.labels_ , alpha=0.4)

plt.autoscale(tight=True)

Matplotlib will chose colors from the k_means.labels array that might look like [0,1,2,0,1,0,0,0,2]

Doing something similar in Bokeh, doesn’t work:

from bokeh.plotting import *

output_notebook()

scatter(X[:,0],X[:,1], c = k_means.labels_ , alpha=0.4,

fill_alpha=0.6, name=“color_scatter_example”, title = “k-means clusters”)

Is there something better than manually mapping the color with an array that explicitly defines colors like:

color_list =

for i in k_means.labels_:

if i == 0:

color_list.append(‘#d18096’)

elif i ==1:

color_list.append(‘#483496’)

else:

color_list.append(‘#00FFD0’)

–

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to bokeh+unsubscribe@continuum.io.

To post to this group, send email to bokeh@continuum.io.

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/af5475c8-cbb8-47ca-8680-3d645302c068%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/groups/opt_out.

⌁   Link to post

End of archived discussion · Replies are disabled