修改Matplotlib PatchCollection中的特定补丁

时间:2019-01-27 12:28:25

标签: python matplotlib

让我们假设我使用了第三方函数<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form method="post" id="modal_form1" action="http://localhost/bookmark/post_crud_model_form"><input type="hidden" id="input1" value="1"> <div class="form-group"> <label for="recipient-name" class="col-form-label">Link</label> <input type="text" class="form-control" id="input1" value="FIRST LINK" name="link"></div> <div class="form-group"> <label for="recipient-name" class="col-form-label">Category</label> <select class="form-control" name="category"> <option>cat 1</option> <option value="1">cat 1</option> <option value="2">cat 2</option> <option value="3">cat 3</option> <option value="4">cat 4</option> </select> </div> <div class="form-group"> <label for="recipient-name" class="col-form-label">Detail</label> <input type="text" class="form-control" id="input1" value="ASDASDASASF" name="detail"></div> <div class="form-group"> <label for="recipient-name" class="col-form-label">Created_at</label> <input type="text" class="form-control" id="input1" value="2019-01-12 22:25:21" name="created_at"></div> <div class="form-group"> <label for="recipient-name" class="col-form-label">Updated_at</label> <input type="text" class="form-control" id="input1" value="2019-01-12 22:25:21" name="updated_at"></div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="submit" class="btn btn-primary" id="send_message">Send message</button> </div> </form>,该函数根据提供的数据向轴添加了一系列补丁。让我们进一步假设我想更改已添加的特定补丁的颜色。我怎么做?

magic_plot(data, ax)

如上所示,我可以从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] 的轴中检索集合,但是如何从此处继续呢?

我假设我需要访问存储在此ax.collections对象中的修补程序列表。但是,在回答类似问题"matplotlib change a Patch in PatchCollection"时,建议使用PatchCollection的子类来跟踪在公共列表属性中添加到其中的补丁,但是鉴于该集合是在第三方功能中创建的,对于我而言,此解决方案不是一种选择。

1 个答案:

答案 0 :(得分:1)

几乎无法更新补丁的形状,如链接的问题所示。但是,您要在此处更改补丁的颜色。这应该容易得多。

在问题的具体示例中,只需设置集合的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()

enter image description here