How do I plot vertical rectangles or highlights that show the missing data areas on this matplotlib time series plot? After importing both matplotlib and pandas, I can generate this image below, but the highlights of missing date intervals are not showing up.
def plot_missing(main):
m_df = fill_missing_timestamps(main[['Date', 'Mean']], 'Mean')
# Find missing 'Mean' values
missing_values = m_df['Mean'].isna()
# Create a mask for missing data
mask = np.zeros_like(m_df['Mean'])
mask[missing_values] = 1
# Identify the start and end indices of missing data intervals
start_indices = np.where(np.diff(mask) == 1)[0] + 1
end_indices = np.where(np.diff(mask) == -1)[0]
# Check if the first or last data point is missing
if missing_values.iloc[0]:
start_indices = np.insert(start_indices, 0, 0)
if missing_values.iloc[-1]:
end_indices = np.append(end_indices, len(main) - 1)
# Create a separate column for the missing data intervals
interval_column = np.zeros_like(m_df['Mean'])
for start, end in zip(start_indices, end_indices):
interval_column[start:end+1] = 1
# Plot the time series with missing data in red and non-missing data in blue
plt.plot(m_df['Date'], m_df['Mean'], color='pink', label='Non-Missing Data')
plt.scatter(m_df['Date'][interval_column == 1], m_df['Mean'][interval_column == 1],
color='red', label='Missing Data')
# Set the title and labels
plt.title('Raw Data \n Missing Data', fontweight='bold', fontsize=18)
plt.xlabel('Date')
plt.ylabel('Mean')
plt.legend()
plt.show()
return
plot_missing(main)
Thank you