[英]How can i update a figure x_range if it's a stringList?
I want to change the x axis of a bokeh plot.我想更改散景图的 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
I can't create a range from a list, how can i do ?我无法从列表中创建范围,我该怎么办?
You have already set the x_range
in the figure constructor and you are trying to set it twice later on.您已经在图形构造函数中设置了x_range
并且您稍后尝试设置它两次。 But then you should use plot.x_range.factors = ["Marseille", "Paris"]
:但是你应该使用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)
Or maybe this is a simpler code that you want (using your source
):或者,这可能是您想要的更简单的代码(使用您的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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.