Nominal/categorical axis on a scatter plot

醉酒当歌 提交于 2019-12-22 15:51:21

问题


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?


回答1:


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




回答2:


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)


来源:https://stackoverflow.com/questions/29081656/nominal-categorical-axis-on-a-scatter-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!