[英]How do I set language of datetime axis in 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)
如果這是相關的,則全局系統區域設置為en-US
:
PS C:\Users\ppatrzyk> GET-WinSystemLocale
LCID Name DisplayName
---- ---- -----------
1033 en-US English (United States)
我正在處理多種語言的繪圖,所以我需要即時更改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)
我遇到了同樣的問題,因為我希望日期以葡萄牙語(pt-PT)顯示。
我發現散景使用文件https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js
來設置日期時間語言。
就我而言,我想生成獨立的.html
文件,所以我選擇在導出文件時設置mode = 'inline'
( output_file('test.html', mode = 'inline')
)以將.js
嵌入到.html
。 但我認為應該可以通過更改.html
<head></head>
中的路徑來使.html
文件加載自定義的bokeh-2.4.2.min.js
。
為了解決我的問題,我編輯了導出的.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"'
我希望這可以幫助任何面臨這個“問題”的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.