Dalek
Dalek

Reputation: 4318

change the positions and the labels for clabel

I am trying to overlay a contour plot on an astronomical image. The following code shows how I generate my contours:

print "contour map "
ny, nx = 50, 50
level=np.array([0.683,0.866,0.954,0.990, 0.997])
print "limits:"
print 
print level
print
bins_x=np.linspace(min(xp),max(xp),nx)
bins_y=np.linspace(min(yp),max(yp),ny)
H, yedges, xedges = np.histogram2d(xp, yp, (bins_x,bins_y),weights=zp)
smooth=0.8
Hsmooth = scipy.ndimage.filters.gaussian_filter(H.T, smooth)
xcenters = (xedges[1:] + xedges[:-1])/2.
ycenters = (yedges[1:] + yedges[:-1])/2.
Xgrid, Ygrid = np.meshgrid(ycenters, xcenters)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1] ]
print len(ra),len(Gcl_1_ra),len(Gcl_2_ra)
# Now we add contours
CS = plt.contour(Xgrid, Ygrid, Hsmooth, levels=level, extent=extent, linewidths=0.4, cmap=cm.Pastel1)

print CS.levels
plt.clabel(CS, CS.levels, colors='red',inline=True, inline_spacing=0.02,fontsize=7, fmt="%0.3f")

print "label coordinate :"
print CS.cl_xy
plt.show()

running the code gives me information about were contour labels are placed:

label coordinate :
[(479.11978392445798, 152.0), (183.33333333333337, 234.5705217606079), (394.86796408013157, 336.0), (462.33333333333337, 156.69957238363236), (183.33333333333337, 232.80998335706244), (399.34062255451977, 296.0), (462.33333333333337, 155.83083286816793), (183.33333333333337, 231.97448760480535), (402.06057288711821, 320.00000000000006), (452.00000000000006, 152.37778562776006), (183.33333333333337, 231.73316562697329), (399.50827125157235, 328.0), (452.00000000000006, 152.25467967915702), (183.33333333333337, 231.68624190906149), (399.44091390280244, 328.0)]

My questions are:

  1. Since all the time the contour's labels are overlap with each other, how could I displace the labels in order that they don't get mixed up? enter image description here

  2. I would like to change the labels for clabel to label=[r'1$\sigma$',r'1.5$\sigma$',r'2$\sigma$',r'2.6$\sigma$',r'3$\sigma$']. How could I do that?

Thanks in advance.

Upvotes: 0

Views: 2738

Answers (1)

mdurant
mdurant

Reputation: 28684

For 1), you can pass a set of x,y values to clabel, where you would like the labels placed with

manual=[(x1,y1),(x2,y2)...

For 2), you can pass fmt as a function to be called with each numerical value, which returns a string for labelling; or a dictionary. For you, it could be

fmt={0.683:r'1$sigma$', 0.866:r'1.5$sigma$', 0.954:r'2$sigma$', 
        0.990:r'2.6$sigma$', 0.997:r'3$sigma$'}

Upvotes: 2

Related Questions