0

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.

2 Answers 2

1

use color='none' to set all colors (facecolor and edgecolor to invisible).

alternatively, you can pass visible=False to the constructor to hide the patch.

Your loop could be:

my_list = [1, 0, 1, 1, 0, 0, 1, 0, 1, 0]
patches = []
fig, ax = plt.subplots()
for i, val in enumerate(my_list):
    p = plt.Rectangle((i, 0), 0.9, 1, angle=0.0,
                                          facecolor='r', edgecolor='w',
                                          linewidth='2.0',
                                          animated=False, visible=bool(val))
    patches.append(p)
    ax.add_patch(p)
Sign up to request clarification or add additional context in comments.

2 Comments

Should it be visible=bool(val) in the Rectangle options? I think if you use the index value it would give true for all but the first index, but we want true/false for 1/0 in list.
yes, of course, sorry wasn't paying enough attention. Updated answer.
1

Sure, you can pass an empty patch to Matplotlib, but I wouldn't unless for some reason you want matplotlib to know about your missing data:

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])
    else:
        self.patches.append(None)

2 Comments

If, for example, I was animating these patches with FuncAnimation, updating the list with new positions for the ones, would matplotlib need to know about the missing data (i.e. the zeros). Would this be a good case for appending None?
Sure thats a reason. OTOH, you'll need to also toggle visibility.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.