Bokeh unable to handle RGB/RGBA tuples

Hey,

I’ve been trying to use bokeh to plot arrays of data with unique colors, but have yet to be successful. Has any one been able to get a working bokeh plot with RGB/RGBA tuples?

Here’s a sample script I was using to plot data. In the matplotlib example I get all the desired colors, but in the bokeh plots they all come out as grey.

import matplotlib.pyplot as plt
import numpy as np

# Import bokeh
import bokeh
import bokeh.plotting

NUM_COLORS = 20

cm = plt.get_cmap('gist_rainbow')

# Define the output file
bokeh.plotting.output_file(filename='quantities.html')

# Define the figure
figure = bokeh.plotting.figure(plot_width=1000, plot_height=600)

# Go through the number of colors
for ix in range(NUM_COLORS):

    # color will now be an RGBA tuple
    color = cm(1.*ix/NUM_COLORS)

    # Define generic data
    x = np.arange(0, 10)
    y = np.arange(0, 10) + ix

    # Plot the data bokeh style
    figure.line(x, y, line_color=color, line_width=2, legend=str(ix))
    figure.scatter(x, y, color=color)

    # Plot the data mpl style
    plt.plot(x, y, color=color)

# Show the bokeh plot
bokeh.plotting.show(figure)

# Show the matplotlib plot
plt.show()

In the “color = cm(1. * ix/NUM_COLORS)” line, try multiplying by 255.​

-Peter

That didn’t seem to work for me either. Any other ideas?

···

On Friday, April 3, 2015 at 3:25:46 PM UTC-7, pwang wrote:

In the “color = cm(1. * ix/NUM_COLORS)” line, try multiplying by 255.​

-Peter

Apparently it’s a bug. I’ve created a related issue on GH that contains the bug details.

In the meanwhile (as posted on that issue) you use bokeh.colors.RGB to explicitly convert the color when passing it to bokeh. Of course, as Peter suggested, you need to multiply you color tuple values by 255 first. Here’s a working version of you example:

import matplotlib.pyplot as plt

import numpy as np

Import bokeh

import bokeh

import bokeh.plotting

from bokeh.colors import RGB

NUM_COLORS = 20

cm = plt.get_cmap(‘gist_rainbow’)

Define the output file

bokeh.plotting.output_file(filename=‘quantities.html’)

Define the figure

figure = bokeh.plotting.figure(plot_width=1000, plot_height=600)

Go through the number of colors

for ix in range(NUM_COLORS):

color will now be an RGBA tuple

color = np.asarray(cm(1.*ix/NUM_COLORS)) * 255

color = RGB(*color).to_hex()

Define generic data

x = np.arange(0, 10)

y = np.arange(0, 10) + ix

Plot the data bokeh style

figure.line(x, y, line_color=color, line_width=2, legend=str(ix))

figure.scatter(x, y, color=color)

Plot the data mpl style

plt.plot(x, y, color=color)

# Show the bokeh plot

bokeh.plotting.show(figure)

``

Best

Fabio

···

On Tuesday, April 7, 2015 at 8:24:16 PM UTC+2, [email protected] wrote:

That didn’t seem to work for me either. Any other ideas?

On Friday, April 3, 2015 at 3:25:46 PM UTC-7, pwang wrote:

In the “color = cm(1. * ix/NUM_COLORS)” line, try multiplying by 255.​

-Peter