Let's assume that I use a third-party function magic_plot(data, ax) that adds a collection of patches to the axes based on the provided data. Let's further assume that I want to change the color of a specific patch that that has been added. How do I do that?
from numpy.random import rand
from matplotlib.collections import PatchCollection
from matplotlib.patches import Circle
import matplotlib.pyplot as plt
def magic_plot(data, ax):
"""
A third-party plotting function, not modifiable by end-users.
"""
lst = []
for i in range(10):
patch = Circle((rand(), rand()), rand())
lst.append(patch)
collection = PatchCollection(lst)
ax.add_collection(collection)
ax = plt.gca()
data = rand(100)
# create the plot:
magic_plot(data, ax)
# obtain the PatchCollection created by magic_plot():
collection = ax.collections[0]
As shown above, I can retrieve the collection from the axes from ax.collections, but how do I proceed from here?
I assume that I need to access the list of patches that are stored in this PatchCollection object. However, in an answer to a similar question "matplotlib change a Patch in PatchCollection", it has been suggested to use a subclass of PatchCollection that tracks the patches that are added to it in a public list attribute, but given that the collection is created in a third-party function, this solution is not an option in my case.
