4

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?

bcf
  • 2,104
  • 1
  • 24
  • 43
  • How about set those value to 0? – Q. Qiao Nov 12 '18 at 17:41
  • @Q.Qiao there are actually no values in the (filtered) dataframe for the overnight hours, but the plot function "connects the dots" between 4pm one day and 8am the next. I would like the plot function to just ignore the overnight time and not plot the connected line (or anything in that time period) – bcf Nov 12 '18 at 17:45
  • there are two ways, either you pad your data with 0 values until the line you circled is stuck to the OX axis or you make a separate figure for each day. – vencaslac Nov 12 '18 at 17:46
  • your x-axis is now datetime. You can make that numerical and add the datetime labels – hootnot Nov 12 '18 at 17:46
  • see, for example: https://stackoverflow.com/questions/10529492/how-do-i-plot-only-weekdays-using-pythons-matplotlib-candlestick – hootnot Nov 12 '18 at 17:52

1 Answers1

0

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

ALollz
  • 57,915
  • 7
  • 66
  • 89