简体   繁体   English

修改Matplotlib PatchCollection中的特定补丁

[英]Modify a specific patch in a Matplotlib PatchCollection

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. 假设我使用了第三方函数magic_plot(data, ax) ,该函数根据提供的数据向轴添加了一系列补丁。 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? 如上所示,我可以从ax.collections的轴中检索集合,但是如何从此处继续呢?

I assume that I need to access the list of patches that are stored in this PatchCollection object. 我假设我需要访问此PatchCollection对象中存储的修补程序列表。 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. 但是,在回答类似的问题“ matplotlib更改PatchCollection中的修补程序”时 ,建议使用PatchCollection的子类来跟踪在公共列表属性中添加到该修补程序的修补程序,但是考虑到创建了该集合在第三方功能中,就我而言,此解决方案不是一种选择。

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. 在问题的具体示例中,只需设置集合的facecolor即可实现。

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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM