是否可以在bokeh中使用逗号而不是点作为十进制分隔符

2024-06-06 02:51:43 发布

您现在位置:Python中文网/ 问答频道 /正文

# Plot
p = figure(title="Weerstand-temperatuurcoefficient",
           x_axis_label="Temperatuur (C)",
           y_axis_label="Weerstand (\u03A9)",
           plot_width=1520,
           plot_height=770
           )

p.yaxis[0].formatter = NumeralTickFormatter(format="0.00")

我现在有这个代码,但我想知道是否可以用逗号代替点来显示小数。我必须这样做,因为学校的规定,否则它将被视为不正确的。我们将不胜感激


Tags: plottitleformatterwidthlabelfigureheightaxis
1条回答
网友
1楼 · 发布于 2024-06-06 02:51:43

BokehJS使用JavaScript,因此默认情况下,您将使用点作为十进制分隔符,并且正如您已经选中的那样,Bokeh格式化程序中没有此选项。但是,您可以使用plot.xaxis.major_label_overrides = {1: "1,0", 2: "2,0"...}使xaxis值看起来像您喜欢的那样

一种方法是接管renderer.data_source.data['x']中的所有值,将它们放在x轴上,然后用“逗号分隔”值替换它们,如下所示(适用于Bokeh v2.1.1):

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

source = ColumnDataSource(dict(x=[1,2,3,4], y=[4,5,5,4]))

plot = figure()
lines = plot.line('x', 'y', source = source)

plot.xaxis.ticker = FixedTicker(ticks=[i for i in source.data['x']])
plot.xaxis.major_label_overrides = {i:'{},0'.format(i) for i in source.data['x']}

show(plot)

如果需要动态更新绘图,则需要在绘图范围上设置CustomJS回调,并相应地更新xaxis值,如下所示:

plot.x_range.js_on_change('end', CustomJS(args={..}, code='...'))

enter image description here

相关问题 更多 >