Для анализа данных я хотел бы создать график с серым полосатым фоном.Серый или белый фон зависит от значения (1 или 0) в другом массиве.Рисунок выглядит следующим образом:
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();
Однако я бы хотел, чтобы ось X указывала даты вместо индекса данных,Когда я попытался заменить idx на date (объект datetime.datetime длиной nr_burst), он выдал ошибку: аргумент float () должен быть строкой или числом, а не datetime.datetime.
Thisбыл код:
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();
Надеюсь, я достаточно ясно объясняю, чего хочу достичь.