Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove leading zero from time of day in Matplotlib DateFormatter

If I use a Matplotlib DateFormatter, like this:

mydateformatter = DateFormatter("%b %d %I:%M %p", self._tz)

I'll get dates like (note the time part has a leading zero):

Nov 27 2011 03:00 PM

Instead, I'd like to lose the leading zero on times (more human that way), like:

Nov 27 2011 3:00 PM

Is there a way to do that?

like image 281
Chelonian Avatar asked Sep 12 '25 19:09

Chelonian


1 Answers

Note: see the edit history to understand the discussion in the comments below. This post has been rewritten to reflect them.

It can't be done using the standard date conversion specifiers, which are listed in the python docs (the same ones as standardized by C). However, there may be platform dependent ways to accomplish this format. A bit of code like this might come in handy:

# Set the default spec to use -- uglier is better than broken.
hour_fmt = '%I'

# If we're running on a platform that has an hour spec w/o leading zero
# then use that one instead.
if sys.platform.startswith('linux'):
    hour_fmt = '%l'
elif sys.platform.startswith('win'):
    hour_fmt = '%#I'
# etc

mydateformatter = DateFormatter("%b %d " + hour_fmt + ":%M %p", self._tz)

I have confirmed %l to work on Linux and the OP has confirmed '%#I` to work on Windows.

like image 110
FatalError Avatar answered Sep 17 '25 19:09

FatalError



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!