如何在散景中设置日期时间轴的语言?

pie*_*eca 7 python locale localization bokeh

我在设置日期时间轴格式的语言时遇到问题bokeh。根据文档“根据当前区域设置”DatetimeTickFormatter生成时间刻度。然而,无论我在 Python 中设置什么语言环境,我都会得到一个英文格式的图:

# jupyter notebook
import locale
locale.setlocale(locale.LC_ALL, 'pl')

import random
from datetime import datetime, date
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
from bokeh.models.formatters import DatetimeTickFormatter

output_notebook()

x_values = [datetime(2018, i, 1) for i in range(6, 13)]
y_values = [random.normalvariate(0, 1) for i in range(6, 13)]

p = figure(plot_width=600, plot_height=300, 
           x_axis_type='datetime', title="test", 
           x_axis_label='x test', y_axis_label='y test')

p.line(
    x=x_values, 
    y=y_values
)

p.xaxis.formatter = DatetimeTickFormatter(months = '%B')

show(p)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果相关,则全局系统区域设置设置为en-US

PS C:\Users\ppatrzyk> GET-WinSystemLocale

LCID             Name             DisplayName
----             ----             -----------
1033             en-US            English (United States)
Run Code Online (Sandbox Code Playgroud)

我正在处理多种语言的绘图,因此我需要locale即时更改。这样做locale.setlocale对我来说效果很好,无论是打印日期到控制台还是使用matplotlib. 如何设置才能使bokeh日期格式正确?

编辑

我得到的最好的解决方法是将日期绘制为数字轴(unix 时间戳),然后用major_label_overrides从 python 的datetime.strftime(). 然而,在这种情况下,缩放到数据点之间的刻度被破坏,因此这远不是一个令人满意的解决方案

x_values = [datetime(2018, i, 1) for i in range(6, 13)]
y_values = [random.normalvariate(0, 1) for i in range(6, 13)]

x_values_timestamp = [int(el.timestamp()) for el in x_values]
x_values_labels = [el.strftime('%B') for el in x_values]

p = figure(plot_width=600, plot_height=300, title="test", 
           x_axis_label='x test', y_axis_label='y test')

p.xaxis.ticker = x_values_timestamp
p.xaxis.major_label_overrides = dict(zip(x_values_timestamp, x_values_labels))

p.line(
    x=x_values_timestamp, 
    y=y_values
)

show(p)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

小智 0

我遇到了同样的问题,因为我希望日期以葡萄牙语(pt-PT)显示。

\n

我发现 bokeh 使用该文件https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js来设置日期时间语言。

\n

就我而言,我想生成独立.html文件,因此我选择mode = 'inline'在导出文件时设置 ( output_file('test.html', mode = 'inline')) 以将文件嵌入.js.html. 但我认为应该可以通过更改 .txt 文件中的路径来使.html文件加载自定义文件。bokeh-2.4.2.min.js.html <head></head>

\n

为了解决我的问题,我编辑了导出的内容(如果不选择则.html编辑自定义)并替换了以下字符串:.jsmode = 'inline'

\n
find = 'locale:"en_US"'\nreplace = 'locale:"pt_PT"'\n\nfind = 'en_US:{date:"%m/%d/%Y",time24:"%I:%M:%S %p",time12:"%I:%M:%S %p",dateTime:"%a %d %b %Y %I:%M:%S %p %Z",meridiem:["AM","PM"],month:{abbrev:"Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec".split("|"),full:"January|February|March|April|May|June|July|August|September|October|November|December".split("|")},day:{abbrev:"Sun|Mon|Tue|Wed|Thu|Fri|Sat".split("|"),full:"Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday".split("|")}}'\nreplace = 'pt_PT:{date:"%d/%m/%Y",time24:"%H:%M:%S",time12:"%I:%M:%S %p",dateTime:"%a %d %b %Y %H:%M:%S %Z",meridiem:["AM","PM"],month:{abbrev:"Jan|Fev|Mar|Abr|Mai|Jun|Jul|Ago|Set|Out|Nov|Dez".split("|"),full:"Janeiro|Fevereiro|Mar\xc3\xa7o|Abril|Maio|Junho|Julho|Agosto|Setembro|Outubro|Novembro|Dezembro".split("|")},day:{abbrev:"Dom|Seg|Ter|Qua|Qui|Sex|S\xc3\xa1b".split("|"),full:"Domingo|Segunda-feira|Ter\xc3\xa7a-feira|Quarta-feira|Quinta-feira|Sexta-feira|S\xc3\xa1bado".split("|")}}'\n\nfind = 'u="Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|year|month|day|hour|minute|second|millisecond"'\nreplace = 'u="Domingo|Segunda-feira|Ter\xc3\xa7a-feira|Quarta-feira|Quinta-feira|Sexta-feira|S\xc3\xa1bado|ano|m\xc3\xaas|dia|hora|minuto|segundo|milissegundo"'\n
Run Code Online (Sandbox Code Playgroud)\n

我希望这可以帮助任何面临这个“问题”的人。

\n