Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set matplotlib.dates.DateFormatter for different languages

I tried

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'))

but yaxis labels are ploted not like 00Z07Oct but 00Z0710□ (probably ploted in my language environment, Japanese, and the characters are garbled.)

On the other hand, I tried,

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

the result is

00z07Oct

This works well.

How can I set matplotlib.dates.DateFormatter for English in different language environment? Any help would be appreciated.


1 Answers

I suspect this has to do with unicode issues, in particular matplotlib.cbook.unicode_safe(). This function is actually run inside DateFormatter on the output of strftime. Try setting the local for everything to see if it helps:

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

If it doesn't, just define a new DateFormatter that doesn't have the cbook.unicode_safe() call:

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

replaced by:

return dt.strftime(fmt)
like image 127
tiago Avatar answered Sep 04 '25 22:09

tiago



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!