【问题标题】:Getting text to display in front of subplot images让文本显示在子图图像前面
【发布时间】:2015-06-26 11:04:57
【问题描述】:

我通过不同的过滤器获得了很多星系图像。每行子图代表一个具有唯一“ID”的新对象。我使用 subplot 函数绘制所有这些图像,但在添加 ID 名称时遇到问题。理想情况下,ID 会延伸到几个子图的前面,但现在它被放在后面(见图)。有谁知道解决这个问题的方法?

plt.close('all')
ID=np.array([])
cata=csv.reader(open('final_final_list.csv',"rU"))
for x in cata:
    ID=np.append(ID,x[0])    
filterset=['ugr','i1','z','Y','J','H','Ks']
test2=np.array([])
for i in range(0,len(ID)):
    for j in range(0,len(filterset)):
       test2=np.append(test2,'filt_image/'+ID[i]+'/'+filterset[j]+'.png')
ID2=np.repeat(ID,7)
filterset2=filterset*64
array=np.arange(0,140,7)
plt.figure()
for i in range(0,140):
    plt.subplot(20,7,i+1)
    plots=img.imread(test2[i])
    plt.imshow(plots)
    plt.axis('off')
    plt.text(0,100,filterset2[i],fontsize='10')

for i in array:
    plt.subplot(20,7,i+1)
    plt.annotate(ID2[i],xy=(0,300),xytext=(0,300),fontsize='10')
plt.show()

【问题讨论】:

    标签: python image text matplotlib subplot


    【解决方案1】:

    每次在图中创建子图时,关联的 Axes 对象都会在幕后附加到列表中。默认情况下,轴是按照它们添加到该列表的顺序绘制的,即创建子图的顺序。在您的示例中,第一列中的每个图都被添加到第二列中的相邻图之前,因此最终后者被绘制在前者之上。

    幸运的是,Matplotlib 有一个 zorder 属性,可以让您控制绘制的顺序。在这种情况下,您需要将第一列中所有轴的 zorder 设置为大于默认值0 的任何整数(更高的值稍后绘制/低于更低的值)。这是一个底部行已调整但顶部行未调整的示例:

    #!/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()
    

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-20
      • 1970-01-01
      • 2020-08-29
      相关资源
      最近更新 更多