问题
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()
回答1:
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()
来源:https://stackoverflow.com/questions/29735743/getting-text-to-display-in-front-of-subplot-images