2

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.

4
  • Is the question how to change the color of a member of a collection? That is totally different from changing the shape/position (for which the linked question gives an answer). Commented Jan 27, 2019 at 12:43
  • @ImportanceOfBeingErnest: Yes, the question is how to change to color of a member of a collection. I thought that the linked question was related enough to that problem. Would you suggest to remove that reference? Commented Jan 27, 2019 at 12:47
  • I edited the question only minimally, but I think this small change would be crucial, please verify that this is the case. Commented Jan 27, 2019 at 12:55
  • @ImportanceOfBeingErnest: Thanks for your edit. I've made the first sentence even more concise. Commented Jan 27, 2019 at 13:04

1 Answer 1

5

Updating the shape of the patches is hardly possible, as shown in the linked question. However, here you want to change the color of a patch. This should be much easier.

In the concrete example from the question, it will be achieved simply by setting the facecolor of the collection.

import numpy as np
np.random.seed(50)

from matplotlib.collections import PatchCollection
from matplotlib.patches import Circle
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors

def magic_plot(data, ax):
    """
    A third-party plotting function, not modifiable by end-users. 
    """
    lst = []
    for i in range(10):
        patch = Circle((np.random.rand(), np.random.rand()), np.random.rand()/9)
        lst.append(patch)
    collection = PatchCollection(lst)
    ax.add_collection(collection)

ax = plt.gca()
data = np.random.rand(100)

# create the plot:
magic_plot(data, ax)
ax.autoscale()

# obtain the PatchCollection created by magic_plot():
collection = ax.collections[0]

n = len(collection.get_paths())
facecolors = collection.get_facecolors()
if len(facecolors) == 1 and n != 1:
    facecolors = np.array([facecolors[0]] * n)

# change the facecolor of the fourth patch (index=3)
facecolors[3] = mcolors.to_rgba("crimson")
collection.set_facecolor(facecolors)

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, so the trick is to retrieve a list with the current colors of the patches by calling get_facecolors() for the collection, modifying those colors that need changing, and then pass the whole list again to set_facecolors(). Thanks, I wouldn't have thought of that.

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.