3

我试过

import locale
locale.setlocale(locale.LC_TIME,'en_US')
tyme = [datetime(2009,10,6,12) + timedelta(hours=6*i) for i in range(5)]
plt.contour(x, tyme, data)
ax=plt.gca()
ax.yaxis.set_major_formatter(matplotlib.dates.DateFormatter('%Hz%d%b'))

但是yaxis标签的绘制不像00Z07Oct而是00Z0710□(可能是在我的语言环境中绘制的,日语,并且字符是乱码。)

另一方面,我尝试过,

 import locale
 locale.setlocale(locale.LC_TIME,'en_US')
 print datetime(2009,10,7,0).strftime(''%Hz%d%b)

结果是

00z07Oct

这很好用。

如何在不同的语言环境中为英语设置 matplotlib.dates.DateFormatter?任何帮助,将不胜感激。

4

2 回答 2

2

我怀疑这与 unicode 问题有关,尤其是matplotlib.cbook.unicode_safe(). 这个函数实际上是DateFormatterstrftime. 尝试为所有内容设置本地,看看是否有帮助:

locale.setlocale(locale.LC_ALL,'en_US')

如果没有,只需定义一个DateFormatter没有cbook.unicode_safe()调用的 new :

return cbook.unicode_safe(dt.strftime(fmt))

取而代之:

return dt.strftime(fmt)
于 2013-01-21T18:53:30.497 回答
0

插入:

locale.setlocale(locale.LC_TIME,'en_US')

或者:

locale.setlocale(locale.LC_ALL, "en_GB.utf8")

在你的身影里面。

如果这不起作用,请尝试在图之前定义格式。例如

import locale
dfmt = dates.DateFormatter('%Hz%d%b')
...
locale.setlocale(locale.LC_ALL, "en_GB.utf8")
ax.yaxis.set_major_formatter(dfmt)
于 2018-08-08T10:49:47.757 回答