How to change matplotlib X axis timestamp format

Sometimes you get weird timestamp format choices in Matplotlib such asĀ 22 19:40 (meaning 19:40 o’clock on the 22nd of whatever month).

This is easy to fix:

import matplotlib.dates as mdates

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))

which will result in this image, for example: