I am adding patches according to a list of ones and zeros (e.g. [1, 0, 1, 1, 0, 0, 1, 0, 1, 0]). I want to add patches where there are ones using matplotlib and leave the zeros empty. However, trying the following code raises a list index out of range error:
fig = plt.figure()
ax = plt.axes()
self.patches = []
for i, val in enumerate(my_list):
if val == 1:
self.patches.append(plt.Rectangle((i, 0), 0.9, 1, angle=0.0,
facecolor='r', edgecolor='w',
linewidth='2.0',
animated=False))
ax.add_patch(self.patches[i])
The only thing I can think of is using an else statement in the code above to add a rectangle with the same colour as the background for the zeros. Is there an empty patch object that one could use instead? I want the patches to be in the same position as the ones in the list.