How to make Matplotlib scatterplots transparent as a group?

后端 未结 4 1500
栀梦
栀梦 2021-02-07 03:19

I\'m making some scatterplots using Matplotlib (python 3.4.0, matplotlib 1.4.3, running on Linux Mint 17). It\'s easy enough to set alpha transparency for each point individuall

4条回答
  •  悲哀的现实
    2021-02-07 03:47

    Yes, interesting question. You can get this scatterplot with Shapely. Here is the code :

    import matplotlib.pyplot as plt
    import matplotlib.patches as ptc
    import numpy as np
    from shapely.geometry import Point
    from shapely.ops import cascaded_union
    
    n = 100
    size = 0.02
    alpha = 0.5
    
    def points():
        x = np.random.uniform(size=n)
        y = np.random.uniform(size=n)
        return x, y
    
    x1, y1 = points()
    x2, y2 = points()
    polygons1 = [Point(x1[i], y1[i]).buffer(size) for i in range(n)]
    polygons2 = [Point(x2[i], y2[i]).buffer(size) for i in range(n)]
    polygons1 = cascaded_union(polygons1)
    polygons2 = cascaded_union(polygons2)
    
    fig = plt.figure(figsize=(4,4))
    ax = fig.add_subplot(111, title="Test scatter")
    for polygon1 in polygons1:
        polygon1 = ptc.Polygon(np.array(polygon1.exterior), facecolor="red", lw=0, alpha=alpha)
        ax.add_patch(polygon1)
    for polygon2 in polygons2:
        polygon2 = ptc.Polygon(np.array(polygon2.exterior), facecolor="blue", lw=0, alpha=alpha)
        ax.add_patch(polygon2)
    ax.axis([-0.2, 1.2, -0.2, 1.2])
    
    fig.savefig("test_scatter.png")
    

    and the result is :

    Test scatter

提交回复
热议问题