Last active
January 25, 2019 20:26
-
-
Save michaelaye/8b5df9fddbb421d752de3bc668973432 to your computer and use it in GitHub Desktop.
How to plot data against locale time of day only.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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