How to make Matplotlib scatterplots transparent as a group?

后端 未结 4 1493
栀梦
栀梦 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

    This is a terrible, terrible hack, but it works.

    You see while Matplotlib plots data points as separate objects that can overlap, it plots the line between them as a single object - even if that line is broken into several pieces by NaNs in the data.

    With that in mind, you can do this:

    import numpy as np
    from matplotlib import pyplot as plt
    
    plt.rcParams['lines.solid_capstyle'] = 'round'
    
    def expand(x, y, gap=1e-4):
        add = np.tile([0, gap, np.nan], len(x))
        x1 = np.repeat(x, 3) + add
        y1 = np.repeat(y, 3) + add
        return x1, y1
    
    x1, y1 = points()
    x2, y2 = points()
    fig = plt.figure(figsize=(4,4))
    ax = fig.add_subplot(111, title="Test scatter")
    ax.plot(*expand(x1, y1), lw=20, color="blue", alpha=0.5)
    ax.plot(*expand(x2, y2), lw=20, color="red", alpha=0.5)
    
    fig.savefig("test_scatter.png")
    plt.show()
    

    And each color will overlap with the other color but not with itself.

    One caveat is that you have to be careful with the spacing between the two points you use to make each circle. If they're two far apart then the separation will be visible on your plot, but if they're too close together, matplotlib doesn't plot the line at all. That means that the separation needs to be chosen based on the range of your data, and if you plan to make an interactive plot then there's a risk of all the data points suddenly vanishing if you zoom out too much, and stretching if you zoom in too much.

    As you can see, I found 1e-5 to be a good separation for data with a range of [0,1].

提交回复
热议问题