python - 组合补丁以获得 matplotlib 中的单一透明度

标签 python matplotlib

我计算绘制在另一幅图像上的类似椭圆的多边形。多边形一起形成一个形状并且必须具有单一透明度。不幸的是,如果我一个接一个地绘制多边形,堆栈会得到不同的透明度。在下面的示例中,我有 3 个多边形,创建 3 个透明度,导致不同的灰色区域而不是一个。有谁知道如何获得单一透明度?

谢谢

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon, Rectangle

def get_ellipse_coords(a=0.0, b=0.0, x=0.0, y=0.0, angle=0.0, k=2):

    pts = np.zeros((360*k+1, 2))
    beta = -angle * np.pi/180.0
    sin_beta = np.sin(beta)
    cos_beta = np.cos(beta)
    alpha = np.radians(np.r_[0.:360.:1j*(360*k+1)])

    sin_alpha = np.sin(alpha)
    cos_alpha = np.cos(alpha)

    pts[:, 0] = x + (a * cos_alpha * cos_beta - b * sin_alpha * sin_beta)
    pts[:, 1] = y + (a * cos_alpha * sin_beta + b * sin_alpha * cos_beta)

    return pts

if __name__ == '__main__':

    fig = plt.figure()
    ax = fig.add_subplot(111, aspect='equal')
    plt.xlim([-5,5])
    plt.ylim([-5,5])

    c='0.5'
    alp=0.5

    ax.add_patch(Rectangle((1.5, -4.0),0.5,8.0,
                           color='lightblue', zorder=1))

    pts = get_ellipse_coords(a=4.0, b=1.0)
    ax.add_patch(Polygon(pts, closed=True, lw=0.5,
                         color=c, alpha=alp,zorder=2))

    pts = get_ellipse_coords(a=4.0, b=1.0, x=1.0, angle=30)
    ax.add_patch(Polygon(pts, closed=True, lw=0.5,
                         color=c, alpha=alp, zorder=2))

    pts = get_ellipse_coords(a=2.0, b=0.25, x=2.0, y=-2.0, angle=250)
    ax.add_patch(Polygon(pts, closed=True, lw=0.5,
                         color=c, alpha=alp, zorder=2))

    plt.show()

第一个答案中建议的 np.concatenate 可以满足我的需要:

    [...]
    ax.add_patch(Rectangle((1.5, -4.0),0.5,8.0,
                           color='lightblue', zorder=1))

    pts1 = get_ellipse_coords(a=4.0, b=1.0)
    pts2 = get_ellipse_coords(a=4.0, b=1.0, x=1.0, angle=30)
    pts3 = get_ellipse_coords(a=2.0, b=0.25, x=2.0, y=-2.0, angle=250)

    stoppoint = np.array([[np.nan,np.nan]])     
    pts = np.concatenate((pts1, stoppoint, pts2))
    pts = np.concatenate((pts, stoppoint, pts3))  

    ax.add_patch(Polygon(pts, closed=True, lw=0.0,
                         color=c, alpha=alp, zorder=2))

最佳答案

像这样:

pts1 = get_ellipse_coords(a=4.0, b=1.0)
pts2 = get_ellipse_coords(a=4.0, b=1.0, x=1.0, angle=30)
stoppoint = np.array([[nan,nan]])     

pts = np.concatenate((pts1, stoppoint, pts2))

ax.add_patch(Polygon(pts, closed=True, lw=0.5,
                     color=c, alpha=alp,zorder=2))

停止点 阻止了连接椭圆的线。

关于python - 组合补丁以获得 matplotlib 中的单一透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38291692/

相关文章:

python - 使用 numpy polyfit 在 python 中使用具有多个变量的 polyfit 将数据拟合到曲线

Python:从excel中的数据创建字典

python - 标记在 seaborn 图中不可见

python - 在 Jupyter Notebook 中的 %matplotlib inline 之后使用 %matplotlib notebook 不起作用

后台 Python Matplotlib 更新图

python - 更改顺序后如何解决numpy ValueError?

matplotlib:获取 `bbox_inches=tight` 的结果边界框

python - 为什么使用 PIL 会出现颜色量化伪影?

python - 数据点颜色从预先指定的点呈放射状变化

python - 多处理 - 共享一个复杂的对象