Search code examples
datetimematplotlibformattingdecimalticker

Matplotlib shows superfluous microsecond precision on x axis ticks


I would like to plot some data as a function of time. I have used matplotlib and datetime, and it works well except for the x axis ticks. The time interval is about an hour, and there are ticks every 10 minutes. However, the ticks do show, for example, 17:50, but 17:50:00.000000 where the zeros overlap with the following tick. How do I get rid of the superfluous zeros?


Solution

  • Without a MWE its hard to know exactly, but I think you need to set the major_formatter. Assuming you have an axis object ax, you can use:

    from matplotlib.dates import DateFormatter, MinuteLocator
    ax.xaxis.set_major_locator( MinuteLocator(byminute=range(0,60,10)) )
    ax.xaxis.set_major_formatter( DateFormatter('%H:%M') )