如果它是stringList,如何更新图形x_range?

时间:2019-04-16 08:44:41

标签: python bokeh

我想更改散景图的x轴。

xrange = ["Marseille", "Lyon"]

plot = figure(x_range=xrange)

plotSource = ColumnDataSource(data=dict(x=xrange, y=[1, 2]))

bars = VBar(x="x", top="y", width=0.1, fill_color="black")

plot.add_glyph(plotSource, bars)

handler = show(plot, notebook_handle=True)

plot.x_range = ["Marseille", "Paris"]  # how can i do that

push_notebook(handle=handler)
ValueError: expected an instance of type Range, got ['Marseille', 'Paris'] of type list

我无法从列表中创建范围,该怎么办?

1 个答案:

答案 0 :(得分:0)

您已经在图形构造函数中设置了x_range,并且稍后尝试设置两次。但是然后您应该使用plot.x_range.factors = ["Marseille", "Paris"]

from bokeh.plotting import show, figure
from bokeh.models import ColumnDataSource, VBar

xrange = ["Marseille", "Lyon"]
plot = figure(x_range = xrange)
plotSource = ColumnDataSource(data = dict(x = xrange, y = [1, 2]))
bars = VBar(x = "x", top = "y", width = 0.1, fill_color = "black")
plot.add_glyph(plotSource, bars)
handler = show(plot, notebook_handle = True)

plot.x_range.factors = ["Marseille", "Paris"]

show(plot)

或者这可能是您想要的更简单的代码(使用source):

from bokeh.plotting import show, figure
from bokeh.models import ColumnDataSource

xrange = ["Marseille", "Lyon"]
p = figure(x_range = xrange)
source = ColumnDataSource(data = dict(x = xrange, y = [1, 2]))
bars = p.vbar(x = "x", top = "y", source = source, width = 0.1, fill_color = "black")

show(p)