For data analysis, I would like to create a graph with grey striped background. Whether the background is grey or white depends on the value (1 or 0) in a different array. The figure looks like this:
import matplotlib.patches as patches
idx = np.linspace(0, nr_burst, nr_burst)
x = total #length nr_burst
y = tide #zeros and ones of length nr_burst
fig, ax = plt.subplots(1, figsize=(15,5))
ax.plot(idx, x)
rect_height = np.max(x)
rect_width = 1
for i, draw_rect in enumerate(y):
if draw_rect:
rect = patches.Rectangle(
(i, 0),
rect_width,
rect_height,
linewidth=1,
edgecolor='lightgrey',
facecolor='lightgrey',
fill=True
)
ax.add_patch(rect)
plt.show();
However, I would like the x-axis to give the dates instead of the index of the data. When I tried replacing idx with dates (a datetime.datetime object of length nr_burst), it gave me the error: float() argument must be a string or a number, not 'datetime.datetime'.
This was the code:
import matplotlib.patches as patches
idx = dates #with length nr_burst as well
x = total #length nr_burst
y = tide #zeros and ones of length nr_burst
fig, ax = plt.subplots(1, figsize=(15,5))
ax.plot(idx, x)
rect_height = np.max(x)
rect_width = 1
for i, draw_rect in enumerate(y):
if draw_rect:
rect = patches.Rectangle(
(dates[i], 0),
rect_width,
rect_height,
linewidth=1,
edgecolor='lightgrey',
facecolor='lightgrey',
fill=True
)
ax.add_patch(rect)
plt.show();
I hope I am being clear enough in the explanation what I want to achieve.