2016-07-10 6 views
2

Ich berechne Ellipse-ähnliche Polygone, die über ein anderes Bild gezeichnet werden. Die Polygone bilden zusammen eine Form und müssen eine einzige Transparenz haben. Wenn ich ein Polygon nach dem anderen zeichne, bekommt der Stapel leider verschiedene Transparenzen. Im Beispiel unten habe ich 3 Polygone, die 3 Transparenzen erzeugen, was zu verschiedenen Graubereichen führt. Weiß jemand, wie man eine einzige Transparenz erhält?Kombinieren von Patches, um einzelne Transparenz in Matplotlib zu erhalten

Danke

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, in der ersten Antwort vorgeschlagen, ist der Trick, ich brauche:

[...] 
    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)) 

Antwort

1

So:

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

Die stoppoint verhindert ein Linie verbindet die Ellipsen.

+0

Ich verstehe es noch nicht, aber das scheint zu funktionieren. Danke! – frits

+0

Es kombiniert nur die Ellipsen zu einem einzelnen Objekt. Dann wird die Transparenz dem gesamten Objekt zugewiesen. – Aguy

+0

Ich habe es, danke. Mein ursprüngliches Problem ist gelöst. – frits

Verwandte Themen