Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改clabel的位置和标签_Python_Matplotlib_Contour - Fatal编程技术网

Python 更改clabel的位置和标签

Python 更改clabel的位置和标签,python,matplotlib,contour,Python,Matplotlib,Contour,我试图在天文图像上叠加一个等高线图。以下代码显示了如何生成轮廓: 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, x

我试图在天文图像上叠加一个等高线图。以下代码显示了如何生成轮廓:

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()
运行代码可以提供有关放置轮廓标签的信息:

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)]
我的问题是

  • 由于等高线的标签一直相互重叠,我如何替换标签以避免混淆?

  • 我想将
    clabel
    的标签更改为
    label=[r'1$\sigma$',r'1.5$\sigma$',r'2$\sigma$',r'2.6$\sigma$',r'3$\sigma$']
    。我怎么能这么做

  • 提前谢谢

    对于1),可以将一组x,y值传递给clabel,您希望标签与

    manual=[(x1,y1),(x2,y2)...
    
    对于2),您可以将fmt作为一个函数传递,以使用每个数值调用,该函数返回一个字符串以进行标记;或者一本字典。对你来说,可能是

    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$'}
    

    为了解决重叠问题,为什么要包括所有级别?或者,您可以对不同的等高线进行颜色编码或线型编码。@mdurant您能否详细解释我如何对不同的等高线进行线型编码?
    [c.set_-linestyle for(c,s)in-zip(CS.collections,['-',':','-']*5)]
    或为等高线调用提供
    linestyles=(…)