Skip to content

Instantly share code, notes, and snippets.

@michaelaye
Last active January 25, 2019 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelaye/8b5df9fddbb421d752de3bc668973432 to your computer and use it in GitHub Desktop.
Save michaelaye/8b5df9fddbb421d752de3bc668973432 to your computer and use it in GitHub Desktop.
How to plot data against locale time of day only.
from matplotlib.dates import DateFormatter
from matplotlib.dates import HourLocator
from matplotlib.dates import date2num
times = pd.date_range("now", periods=24, freq="2h")
data = np.arange(len(times))
df = pd.DataFrame({"datetime": times, "data": data})
times_numbers = df.datetime.map(date2num)
df["LT"] = times_numbers % 1 + int(times_numbers[0])
fig, ax = plt.subplots()
df.plot(x="LT", y="data", style=".", ax=ax)
ax.xaxis.set_major_formatter(DateFormatter("%H:%M"))
ax.xaxis.set_major_locator(HourLocator(interval=3))
ax.grid(which="major")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment