Вот моя попытка. Код предназначен для данных и может работать, а может и не работать с другими данными.
data = """
time,seriesA,seriesB,Condition
0.00 , 10 , 2 ,A
0.12 , 4 , 20 ,A
0.24 , 14 , 16 ,A
0.36 , 5 , 16 ,A
0.48 , 8 , 13 ,A
0.6 , 5 , 16 ,B
0.72 , 17 , 3 ,B
0.84 , 8 , 20 ,B
0.96 , 3 , 3 ,B
1.08 , 18 , 20 ,C
1.20 , 11 , 13 ,C
1.32 , 8 , 10 ,C
1.44 , 17 , 11 ,A
1.56 , 15 , 2 ,A
1.68 , 1 , 1 ,A
1.8 , 20 , 2 ,A
1.92 , 11 , 19 ,B
2.04 , 8 , 13 ,B
2.16 , 5 , 8 ,B
2.28 , 14 , 18 ,B
2.40 , 7 , 2 ,B
2.52 , 12 , 5 ,C
2.64 , 4 , 11 ,C
2.76 , 1 , 13 ,C
2.88 , 16 , 9 ,C
3 , 14 , 17 ,C
"""
with open("time_series_with_axspan.txt", "wt") as f:
f.write(data)
d = pd.read_csv("time_series_with_axspan.txt")
d.plot(x='time', y=['seriesA', 'seriesB'])
ax = plt.gca()
# create spans; is there an easier way of doing this in pandas?
condition = d['Condition']
current_c = condition[0]
spans = [[0, 0]]
for i, c in enumerate(condition):
if c != current_c:
# change to i-1 if gap between two conditions is to be left empty
spans[-1][-1] = i
spans.append([i, None])
current_c = c
# assumes that the last condition is not on its own but same as previous
spans[-1][-1] = len(condition)-1
# The following works since are three unique conditions appearing one after
# the other. Any three colors will work for example
# color=cycle(['r', 'g', 'b'])
from itertools import cycle
colors = cycle(plt.rcParams['axes.prop_cycle'].by_key()['color'][-3:])
for span in spans:
ax.axvspan(d.iloc[span[0], 0], d.iloc[span[1], 0], color=next(colors))
Надеюсь, это поможет.