Clip an image using several patches in matplotlib

后端 未结 1 2004
孤街浪徒
孤街浪徒 2021-01-05 16:05

I have a plot in pylab which I want to clip to the borders of a map of the UK.

I\'ve also made a series of patches which contain the outlines of each country: one f

相关标签:
1条回答
  • 2021-01-05 16:46

    I think you can do this by making multiple scatter plots, clipping each one with a unique patch (eg one has England, one has Ireland, etc). Though this might not be what you asked for, ie "Does anyone know how I can clip using an 'OR' type statement?", it should have the same effect:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.patches as patches
    
    np.random.seed(101)
    x = np.random.random(100)
    y = np.random.random(100)
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    imForEngland = ax.scatter(x,y)
    fig.savefig('beforeclip.png')
    imForWales = ax.scatter(x,y)
    england = patches.Circle((.75,.75),radius=.25,fc='none')
    wales = patches.Circle((.25,.25),radius=.25,fc='none')
    ax.add_patch(england)
    ax.add_patch(wales)
    imForEngland.set_clip_path(england)
    imForWales.set_clip_path(wales)
    
    fig.savefig('afterclip.png')
    

    Before the patches: enter image description here After the patches: enter image description here

    0 讨论(0)
提交回复
热议问题