Python 如何从matplotlib中的子批次访问特定修补程序的属性

Python 如何从matplotlib中的子批次访问特定修补程序的属性,python,matplotlib,patch,Python,Matplotlib,Patch,我遇到了一个关于如何从子地块轴检索特定面片对象的问题。 例如,我在一个函数中创建了一个子图,其中包含许多面片。多边形对象(我为每个对象指定了不同的标签)。在我通过add_子图将这个子图添加到一个图形中之后,我似乎失去了对这些面片的访问。我在子图中创建的多边形对象。 我知道我可以使用findobj()方法获取对象,但是,它只返回对象的类型及其内存地址。我可以更改所有对象的facecolor,但我真正需要的是按名称或标签访问一个特定对象,例如更改一个面片的颜色。PolyGon而不是所有面片的颜色。

我遇到了一个关于如何从子地块轴检索特定面片对象的问题。 例如,我在一个函数中创建了一个子图,其中包含许多面片。多边形对象(我为每个对象指定了不同的标签)。在我通过add_子图将这个子图添加到一个图形中之后,我似乎失去了对这些面片的访问。我在子图中创建的多边形对象。 我知道我可以使用findobj()方法获取对象,但是,它只返回对象的类型及其内存地址。我可以更改所有对象的facecolor,但我真正需要的是按名称或标签访问一个特定对象,例如更改一个面片的颜色。PolyGon而不是所有面片的颜色。 如果有人知道如何做到这一点,我将不胜感激。我的脚本附在下面

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.gridspec as gridspec
import numpy as np


def drawEI(ax) :
    # draw Endcap
    x0 = np.array([0.5, 0.5, 0.5, 0.5])
    y0 = np.array([0.5, 0.5, 0.5, 0.5])

    step = 0.08

    xy = np.zeros((2, 4))
    print(xy)
    # theta = [for i in range(16) : ]
    theta = [345, 15, 30, 60, 75, 105, 120, 150, 165, 195, 210, 240, 255, 285, 300, 330]
    nSector = 16
    nEta = 4

    chamberPlot = {}

    for sector in range(nSector):
        for eta in range(nEta):
            # skip eta = 3
            if eta == 2 : continue
            if eta == 3 and sector%2 == 0 : continue
            #if eta == 4 :
            # print (sector, eta)
            # x1[sector][eta] = step*(1+eta)*np.cos(np.pi*theta[sector]/360.)
            # y1[sector][eta] = step*(1+eta)*np.sin(np.pi*theta[sector]/360.)
            # create polygon coordinate by numpy
            # xy = np.arange(8).reshape(4,2)
            x1 = np.array([step * (1 + eta) * np.cos(np.pi * theta[sector] / 180.),
                           step * (2 + eta) * np.cos(np.pi * theta[sector] / 180.),
                           step * (2 + eta) * np.cos(np.pi * theta[(sector + 1) % 16] / 180.),
                           step * (1 + eta) * np.cos(np.pi * theta[(sector + 1) % 16] / 180.)])
            y1 = np.array([step * (1 + eta) * np.sin(np.pi * theta[sector] / 180.),
                           step * (2 + eta) * np.sin(np.pi * theta[sector] / 180.),
                           step * (2 + eta) * np.sin(np.pi * theta[(sector + 1) % 16] / 180.),
                           step * (1 + eta) * np.sin(np.pi * theta[(sector + 1) % 16] / 180.)])
            # print (x1+=x0,y1+=y0)
            xy[0] = x1 + x0
            xy[1] = y1 + y0
            newxy = xy.transpose()
            print(newxy.tolist())
            # ax.add_patch(patches.Polygon(xy=list(zip(np.add(x1+x0),np.add(y1+y0))), fill=False))
            index = sector + sector * eta
            print(index)
            chamberPlot[index] = patches.Polygon(newxy.tolist(), edgecolor='black', facecolor='green')
            chamberPlot[index].set_label('EI_%s_%s'%(eta,sector))
            # ax.add_patch(patches.Polygon(newxy.tolist(), edgecolor = 'black', facecolor = 'green'))
            ax.add_patch(chamberPlot[index])

    return ax

def drawEM(ax) :
    # draw Endcap
    x0 = np.array([0.5, 0.5, 0.5, 0.5])
    y0 = np.array([0.5, 0.5, 0.5, 0.5])

    step = 0.07

    xy = np.zeros((2, 4))
    print(xy)
    # theta = [for i in range(16) : ]
    theta = [345, 15, 30, 60, 75, 105, 120, 150, 165, 195, 210, 240, 255, 285, 300, 330]
    nSector = 16
    nEta = 5

    chamberPlot = {}

    for sector in range(nSector):
        for eta in range(nEta):
            # print (sector, eta)
            # x1[sector][eta] = step*(1+eta)*np.cos(np.pi*theta[sector]/360.)
            # y1[sector][eta] = step*(1+eta)*np.sin(np.pi*theta[sector]/360.)
            # create polygon coordinate by numpy
            # xy = np.arange(8).reshape(4,2)
            x1 = np.array([step * (1 + eta) * np.cos(np.pi * theta[sector] / 180.),
                           step * (2 + eta) * np.cos(np.pi * theta[sector] / 180.),
                           step * (2 + eta) * np.cos(np.pi * theta[(sector + 1) % 16] / 180.),
                           step * (1 + eta) * np.cos(np.pi * theta[(sector + 1) % 16] / 180.)])
            y1 = np.array([step * (1 + eta) * np.sin(np.pi * theta[sector] / 180.),
                           step * (2 + eta) * np.sin(np.pi * theta[sector] / 180.),
                           step * (2 + eta) * np.sin(np.pi * theta[(sector + 1) % 16] / 180.),
                           step * (1 + eta) * np.sin(np.pi * theta[(sector + 1) % 16] / 180.)])
            # print (x1+=x0,y1+=y0)
            xy[0] = x1 + x0
            xy[1] = y1 + y0
            newxy = xy.transpose()
            print(newxy.tolist())
            # ax.add_patch(patches.Polygon(xy=list(zip(np.add(x1+x0),np.add(y1+y0))), fill=False))
            index = sector + sector * eta
            print(index)
            chamberPlot[index] = patches.Polygon(newxy.tolist(), edgecolor='black', facecolor='green')
            chamberPlot[index].set_label('EM_%s_%s'%(eta,sector))
            # ax.add_patch(patches.Polygon(newxy.tolist(), edgecolor = 'black', facecolor = 'green'))
            ax.add_patch(chamberPlot[index])

    return ax

def drawEO(ax) :
    # draw Endcap
    x0 = np.array([0.5, 0.5, 0.5, 0.5])
    y0 = np.array([0.5, 0.5, 0.5, 0.5])

    step = 0.07

    xy = np.zeros((2, 4))
    print(xy)
    # theta = [for i in range(16) : ]
    theta = [345, 15, 30, 60, 75, 105, 120, 150, 165, 195, 210, 240, 255, 285, 300, 330]
    nSector = 16
    nEta = 6

    chamberPlot = {}

    for sector in range(nSector):
        for eta in range(nEta):
            # print (sector, eta)
            # x1[sector][eta] = step*(1+eta)*np.cos(np.pi*theta[sector]/360.)
            # y1[sector][eta] = step*(1+eta)*np.sin(np.pi*theta[sector]/360.)
            # create polygon coordinate by numpy
            # xy = np.arange(8).reshape(4,2)
            x1 = np.array([step * (1 + eta) * np.cos(np.pi * theta[sector] / 180.),
                           step * (2 + eta) * np.cos(np.pi * theta[sector] / 180.),
                           step * (2 + eta) * np.cos(np.pi * theta[(sector + 1) % 16] / 180.),
                           step * (1 + eta) * np.cos(np.pi * theta[(sector + 1) % 16] / 180.)])
            y1 = np.array([step * (1 + eta) * np.sin(np.pi * theta[sector] / 180.),
                           step * (2 + eta) * np.sin(np.pi * theta[sector] / 180.),
                           step * (2 + eta) * np.sin(np.pi * theta[(sector + 1) % 16] / 180.),
                           step * (1 + eta) * np.sin(np.pi * theta[(sector + 1) % 16] / 180.)])
            # print (x1+=x0,y1+=y0)
            xy[0] = x1 + x0
            xy[1] = y1 + y0
            newxy = xy.transpose()
            print(newxy.tolist())
            # ax.add_patch(patches.Polygon(xy=list(zip(np.add(x1+x0),np.add(y1+y0))), fill=False))
            index = sector + sector * eta
            print(index)
            chamberPlot[index] = patches.Polygon(newxy.tolist(), edgecolor='black', facecolor='green')
            chamberPlot[index].set_label('EO_%s_%s'%(eta,sector))
            # ax.add_patch(patches.Polygon(newxy.tolist(), edgecolor = 'black', facecolor = 'green'))
            ax.add_patch(chamberPlot[index])

    return ax



fig = plt.figure(constrained_layout=False)

spec2 = gridspec.GridSpec(ncols=2, nrows=2, figure=fig)

axEM = fig.add_subplot(spec2[1,0])
axEM.set_xticklabels([])
axEM.set_yticklabels([])
axEM.axis('off')

axEM = drawEM(axEM)

axEO = fig.add_subplot(spec2[0,1])
axEO.set_xticklabels([])
axEO.set_yticklabels([])
axEO.axis('off')

axEO = drawEO(axEO)

axEI = fig.add_subplot(spec2[0,0])
axEI.set_xticklabels([])
axEI.set_yticklabels([])
axEI.axis('off')

axEI = drawEI(axEI)


objs = axEI.findobj(patches.Polygon)
print (objs)
for obj in objs :
    #print (obj.label())
    obj.set_facecolor('red')  # this is ok 

plt.subplots_adjust(wspace=0, hspace=0)
plt.show()  

我的问题是如何更改子地块axEM中一个特定多边形“BM_2_5”的面部颜色?

使用axis对象和面片索引,您可以通过以下方式实现所需:

import matplotlib.pyplot as plt
import matplotlib.patches as ptc
import numpy as np

fig, ax = plt.subplots()
pCol = ['red', 'blue']
for i in range(2):
    ax.add_patch(ptc.Polygon(np.random.rand(3, 2), fc=pCol[i]))
ax.patches[0].set_facecolor((0, 1, 0, 1))
plt.show()

请注意,没有红色多边形是如何打印的

编辑:记住你的评论

import matplotlib.pyplot as plt
import matplotlib.patches as ptc
import numpy as np

fig, ax = plt.subplots()
pLab = ['First', 'Second']
pCol = ['red', 'blue']
for i in range(2):
    ax.add_patch(ptc.Polygon(np.random.rand(3, 2), fc=pCol[i], label=pLab[i]))
for patch in ax.patches:
    if patch.get_label() == 'First':
        patch.set_facecolor('green')
plt.show()

编辑:从函数返回轴应该是正常的

import matplotlib.pyplot as plt
import matplotlib.patches as ptc
import numpy as np


def drawPatches(ax):
    for i in range(2):
        ax.add_patch(ptc.Polygon(np.random.rand(3, 2), fc=pCol[i],
                                 label=pLab[i]))
    return ax


fig, ax = plt.subplots()
pLab = ['First', 'Second']
pCol = ['red', 'blue']
ax = drawPatches(ax)
for patch in ax.patches:
    if patch.get_label() == 'First':
        patch.set_facecolor('green')
plt.show()

非常感谢@Patol75,很高兴通过索引访问。但是,我正在查看是否有一种方法可以通过我创建补丁时提供的名称/标签访问补丁。否则,必须有人提前知道这些补丁的确切顺序才能更改属性。再次感谢。事实上,我真正的问题是我无法从函数中取出补丁标签。当我将下面两行代码添加到代码中时,得到了一个空字符串。所以这就变成了一个关于变量作用域的问题。我认为Axis对象仍然会保留关于包含的补丁的所有信息对于axEM.patches中的补丁:print(patch.get_label())``这个循环确实就在
axEM=paurem(axEM)
之后?另外,请看我的第二次编辑。你是对的,我最终运行了我的代码,得到了我想看到的结果。非常感谢你。