获取文本显示在子图像前面 [英] Getting text to display in front of subplot images

查看:94
本文介绍了获取文本显示在子图像前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 subplot function but am having trouble adding the ID name. Ideally the ID would stretch in front of several subplots but at the moment it is placed behind (see picture). Does anyone know of a way to fix this?

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()


推荐答案

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

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.

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

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()

这篇关于获取文本显示在子图像前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆