user1251007
user1251007

Reputation: 16721

Nominal/categorical axis on a scatter plot

How is it possible to generate a nominal/categorical axis (entries like a, b, c instead of 1,2,3) on a scatter plot in Bokeh?

Imagine that the following data should be plotted:

a  0.5
b  10.0
c  5.0

I tried the following:

import bokeh.plotting as bk

# output to static HTML file
bk.output_file("scatter.html", title="scatter plot example")

x = ['a', 'b', 'c']
y = [0.5, 10.0, 5.0]

p = bk.figure(title = "scatter")
p.circle(x = x, y = y)
bk.show(p)

However, this generates an empty plot. If the x data is changed to x = [1, 2, 3] everything gets plotted as expected.

What can I do to have a, b, c on the x axis?

Upvotes: 2

Views: 2363

Answers (2)

bigreddot
bigreddot

Reputation: 34568

You need to supply the list of categories as the x_range or y_range explicitly. See:

http://docs.bokeh.org/en/latest/docs/gallery/categorical.html

Upvotes: 2

user1251007
user1251007

Reputation: 16721

Based on bigreddot's answer, x_range needs to be set explicitly as follows:

p = bk.figure(title = "scatter", x_range = x)

This is the complete example:

import bokeh.plotting as bk

# output to static HTML file
bk.output_file("scatter.html", title="scatter plot example")

x = ['a', 'b', 'c']
y = [0.5, 10.0, 5.0]

p = bk.figure(title = "scatter", x_range = x)
p.circle(x = x, y = y)
bk.show(p)

Upvotes: 4

Related Questions