Getting text to display in front of subplot images

后端 未结 1 1978
無奈伤痛
無奈伤痛 2021-01-15 20:02

Ive got a lot of images of galaxies through different filters. each line of subplots represents a new object with a unique \'ID\'. Im plotting all of these images using the

相关标签:
1条回答
  • 2021-01-15 20:30

    Each time you create a subplot in a figure the associated Axes object is appended to a list behind the scenes. By default the axes are drawn in the order that they were added to that list, i.e., the order in which the subplots were created. In your example, each of the plots in column one is added immediately before its neighbour in column two, so ultimately the latter gets drawn over the former.

    Fortunately, Matplotlib has a zorder property to give you control over the order in which things are drawn. In this case you need to set the zorder for all the axes in column one to any integer greater than the default value of 0 (higher values are drawn later/above lower values). Here's an example where the bottom row has been adjusted but the top row hasn't:

    #!/usr/bin/env python
    
    import matplotlib.pyplot as plt
    import numpy as np
    
    Z = np.random.random((5,5))
    txt = "abcdefghijlkmopqrstuvwxyz" * 2
    
    F, A = plt.subplots(ncols=2,nrows=2)
    
    for ax in A.flat:
        ax.imshow(Z,interpolation="nearest")
        ax.axis("off")
    
    for ax in A[:,0].flat:
        ax.text(0,0,txt)
    # Uncomment the next line to adjust all plots in column one.
    #    ax.set_zorder(1)
    
    # The next line just adjusts one plot, as an example.
    A[1,0].set_zorder(1)
    
    plt.show()
    

    enter image description here

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