Selective patterns with Matplotlib imshow

后端 未结 1 1548
抹茶落季
抹茶落季 2021-01-14 08:57

Is there a way to place custom patterns into selected areas on an imshow graph? To be precise, I need to make it so that, in addition to the numerical-data-carrying colored

相关标签:
1条回答
  • 2021-01-14 09:13

    There are several ways to do this, which is better will depend on if you need to mark large regions or scattered individual pixels.

    If you need to mark large regions, you can do this by added rectangles over the image:

    import matplotlib as mpl
    import matplotlib.pyplot as plt
    
    ax = plt.gca()
    ax.imshow(rand(50,50))
    ax.add_patch(mpl.patches.Rectangle((2,2),20,20,hatch='//////////',fill=False,snap=False))
    plt.draw()
    

    Rectangle (doc) a variety of hash options. This is just adding additional artists on top of the image, they will not affect the data-color mapping in any way. The large number of / increases the density of the hash marks, which may be necessary to acctually see the hashing with small boxes.

    ex:

    from matplotlib.pylab import * # mostly to get rand()
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    
    ax = plt.gca()
    ax.imshow(rand(50,50),interpolation='nearest')
    for i,j in np.floor(50*rand(10,2)).astype('int'):
        ax.add_patch(mpl.patches.Rectangle((i-.5, j-.5), 1, 1, hatch='///////', fill=False, snap=False))
    
    plt.draw()
    

    enter image description here If you just need to mark a few pixels here and you might be able to get away with just plotting (using plot([x],[y],marker='x') and maybe playing with the marker size).

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