С помощью plt.barh
вы можете указать все начальные позиции, используя 10 минут в качестве ширины полос. Раскрашивание нежелательных позиций цветом 'none':
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
dates = pd.date_range(start='2020-02-14 20:30', end='2020-02-24', freq='10min')
data = pd.DataFrame({'Timestamp': dates,
'Validity': (np.round(np.random.uniform(0, .02, len(dates)).cumsum()) % 2).astype(bool)})
color = 'dodgerblue'
plt.barh(y=1, left=data['Timestamp'], width=1/24/6, height=0.3,
color=['none' if not val else color for val in data['Validity']])
plt.axhline(1, color=color)
plt.barh(y=0, left=data['Timestamp'], width=1/24/6, height=0.3, color=['none' if val else color for val in data['Validity']])
plt.axhline(0, color=color)
plt.gca().xaxis.set_major_locator(mdates.AutoDateLocator())
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%b'))
plt.yticks([0, 1], ['False', 'True'])
# plt.xticks(rotation=30)
plt.margins(y=0.4)
plt.ylabel('Validity')
plt.xlabel('Timestamp')
plt.tight_layout()
plt.show()
Here is a slightly more involved approach, that permits irregular timestamps.
First convert the boolean column to integers (0
for false
and 1
for true
). Then calculate the difference between successive entries. This shows zeros while the values stay the same, a -1
where a False
flips to a True
and a +1
when flipping back to False
. The indices where this happens can be used to index the timestamps. To avoid problems with the first and last index, a dummy value needs to be appended at the start end of the array.
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
dates = pd.date_range(start='2020-02-14 20:30', end='2020-02-24', freq='10min')
data = pd.DataFrame({'Timestamp': dates,
'Validity': (np.round((np.random.uniform(0, .02, len(dates))).cumsum()) % 2).astype(bool)})
tm = data['Timestamp'].to_numpy()
tm = np.append(tm, tm[-1])
color = 'navy'
steps_true = np.diff(np.concatenate([[False], data['Validity'], [False]]).astype(int))
plt.barh(y=1, left=tm[np.flatnonzero(steps_true == 1)], height=0.2,
width=tm[np.flatnonzero(steps_true == -1)] - tm[np.flatnonzero(steps_true == 1)], color=color)
plt.axhline(1, color=color)
steps_false = np.diff(np.concatenate([[True], data['Validity'], [True]]).astype(int))
plt.barh(y=0, left=tm[np.flatnonzero(steps_false == -1)], height=0.2,
width=tm[np.flatnonzero(steps_false == 1)] - tm[np.flatnonzero(steps_false == -1)], color=color)
plt.axhline(0, color=color)
plt.gca().xaxis.set_major_locator(mdates.AutoDateLocator())
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%b'))
plt.yticks([0, 1], ['False', 'True'])
plt.margins(y=0.4)
plt.ylabel('Validity')
plt.xlabel('Timestamp')
plt.tight_layout()
plt.show()
второй сюжет