[英]How do I set language of datetime axis in bokeh?
I have trouble with setting language in which datetime axis is formatted in bokeh
.我无法设置日期时间轴在bokeh
中格式化的语言。 According to documentation , DatetimeTickFormatter
produces time ticks 'according to the current locale' .根据文档, DatetimeTickFormatter
会“根据当前语言环境”生成时间刻度。 However, whatever locale I set in Python, I get a plot formatted in English:但是,无论我在 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)
If that's relevant, global system locale is set to en-US
:如果这是相关的,则全局系统区域设置为en-US
:
PS C:\Users\ppatrzyk> GET-WinSystemLocale
LCID Name DisplayName
---- ---- -----------
1033 en-US English (United States)
I am working with plots in multiple languages so I need to change locale
on the fly .我正在处理多种语言的绘图,所以我需要即时更改locale
。 Doing that through locale.setlocale
has worked fine for me with both printing dates to console and with matplotlib
.通过locale.setlocale
这样做对我来说效果很好,无论是打印日期到控制台还是使用matplotlib
。 How can I set it in bokeh
such that dates are formatted correctly?如何在bokeh
中设置它以便日期格式正确?
EDIT :编辑:
The best workaround I got is to plot dates as numeric axis (unix timestamp) and then use major_label_overrides
to replace ticks with correctly formatted dates obtained from python's datetime.strftime()
.我得到的最佳解决方法是将日期绘制为数字轴(unix 时间戳),然后使用major_label_overrides
将刻度替换为从 python 的datetime.strftime()
获得的正确格式的日期。 However, in this case zooming to ticks in between data points is broken, so this is far from being a satisfactory solution :但是,在这种情况下,放大到数据点之间的刻度会被破坏,因此这远不是一个令人满意的解决方案:
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)
I faced the same problem as I wanted the dates be displayed in portuguese of Portugal (pt-PT).我遇到了同样的问题,因为我希望日期以葡萄牙语(pt-PT)显示。
I found out that bokeh uses the file https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js
to set the datetime language.我发现散景使用文件https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js
来设置日期时间语言。
In my case, I wanted to generate standalone .html
files, so I opted to set mode = 'inline'
when exporting the file ( output_file('test.html', mode = 'inline')
) to embed the .js
into the .html
.就我而言,我想生成独立的.html
文件,所以我选择在导出文件时设置mode = 'inline'
( output_file('test.html', mode = 'inline')
)以将.js
嵌入到.html
。 But I assume that it should be possible to make the .html
file load a custom bokeh-2.4.2.min.js
by changing its path in the .html
<head></head>
.但我认为应该可以通过更改.html
<head></head>
中的路径来使.html
文件加载自定义的bokeh-2.4.2.min.js
。
To solve my problem, I edited the exported .html
(edit the custom .js
if you don't choose mode = 'inline'
) and replaced the following strings:为了解决我的问题,我编辑了导出的.html
(如果您不选择mode = 'inline'
,请编辑自定义.js
)并替换以下字符串:
find = 'locale:"en_US"'
replace = 'locale:"pt_PT"'
find = '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("|")}}'
replace = '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ço|Abril|Maio|Junho|Julho|Agosto|Setembro|Outubro|Novembro|Dezembro".split("|")},day:{abbrev:"Dom|Seg|Ter|Qua|Qui|Sex|Sáb".split("|"),full:"Domingo|Segunda-feira|Terça-feira|Quarta-feira|Quinta-feira|Sexta-feira|Sábado".split("|")}}'
find = 'u="Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|year|month|day|hour|minute|second|millisecond"'
replace = 'u="Domingo|Segunda-feira|Terça-feira|Quarta-feira|Quinta-feira|Sexta-feira|Sábado|ano|mês|dia|hora|minuto|segundo|milissegundo"'
I hope this helps anyone facing this "issue".我希望这可以帮助任何面临这个“问题”的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.