Pandas plotting--ignore time range

Viewed 980

Let's say I have one-minute data during business hours of 8am to 4pm over three days. I would like to plot these data using the pandas plot function:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(51723)
dates = pd.date_range("11/8/2018", "11/11/2018", freq = "min")
df = pd.DataFrame(np.random.rand(len(dates)), index = dates, columns = ['A'])
df = df[(df.index.hour >= 8) & (df.index.hour <= 16)]  # filter for business hours

fig, ax = plt.subplots()
df.plot(ax = ax)
plt.show()

However, the plot function also includes overnight hours in the plot, resulting in unintended plotting during this time:

bad plotting

I would the data to be plotted contiguously, ignoring the overnight time (something like this): good plotting

What is a good way to plot only the intended hours of 8am to 4pm?

1 Answers

This can be done by plotting each date on a different axis. But things like the labels will get cramped in certain cases.

import datetime
import matplotlib.pyplot as plt

pdates = np.unique(df.index.date)  # Unique Dates 

fig, ax = plt.subplots(ncols=len(pdates), sharey=True, figsize=(18,6))

# Adjust spacing between suplots 
# (Set to 0 for continuous, though labels will overlap)
plt.subplots_adjust(wspace=0.05)

# Plot all data on each subplot, adjust the limits of each accordingly
for i in range(len(pdates)):
    df.plot(ax=ax[i], legend=None)
    # Hours 8-16 each day:
    ax[i].set_xlim(datetime.datetime.combine(pdates[i], datetime.time(8)), 
                   datetime.datetime.combine(pdates[i], datetime.time(16)))

    # Deal with spines for each panel
    if i !=0:
        ax[i].spines['left'].set_visible(False)
        ax[i].tick_params(right=False,
                  which='both',
                  left=False,
                  axis='y')
    if i != len(pdates)-1:
        ax[i].spines['right'].set_visible(False)
plt.show()

enter image description here

Related