我怎样才能保持绿色和透明度?

How can I do to keep the green color with the transparency?

您好,我正在使用 Python,尤其是 matplotlib 库来绘制一些矩形。这是我的代码:

def draw_rectangle():
    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.widgets import RectangleSelector

    xdata = np.linspace(0, 9 * np.pi, num=301)
    ydata = np.sin(xdata)

    fig, ax = plt.subplots()
    ax.plot(xdata, ydata)

    def line_select_callback(eclick, erelease):
        x1, y1 = eclick.xdata, eclick.ydata
        x2, y2 = erelease.xdata, erelease.ydata

        rect = plt.Rectangle((min(x1, x2), min(y1, y2)), np.abs(x1 - x2), np.abs(y1 - y2))
        ax.add_patch(rect)

    rs = RectangleSelector(ax, line_select_callback,
                           drawtype='box', useblit=False, button=[1],
                           minspanx=5, minspany=5, spancoords='pixels',
                           interactive=True,
                           rectprops=dict(facecolor='green', edgecolor='black', alpha=0.8, fill=True))

    plt.show()

if __name__ == '__main__':
    draw_rectangle()

问题如下:我想在释放鼠标按钮时保持绿色和透明度。

我该怎么做?

非常感谢!

单独定义矩形属性,然后将它们用于选择器和面片。

def draw_rectangle():
    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.widgets import RectangleSelector

    xdata = np.linspace(0, 9 * np.pi, num=301)
    ydata = np.sin(xdata)

    fig, ax = plt.subplots()
    ax.plot(xdata, ydata)

    rectprops=dict(facecolor='green',
                   edgecolor='black',
                   alpha=0.8, fill=True)    #*******

    def line_select_callback(eclick, erelease, props=rectprops):
        x1, y1 = eclick.xdata, eclick.ydata
        x2, y2 = erelease.xdata, erelease.ydata

        rect = plt.Rectangle((min(x1, x2), min(y1, y2)),
                             np.abs(x1 - x2),
                             np.abs(y1 - y2),
                             **props)    #*******
        ax.add_patch(rect)

    rs = RectangleSelector(ax, line_select_callback,
                           drawtype='box', useblit=False, button=[1],
                           minspanx=5, minspany=5, spancoords='pixels',
                           interactive=True,
                           rectprops=rectprops)    #*******

    plt.show()
    plt.close()