【问题标题】:How to obtain the contour plot data for each scatter points?如何获取每个散点的等高线图数据?
【发布时间】:2015-10-29 23:09:44
【问题描述】:

我绘制了一个等高线图作为背景,代表该地区的高度。

并设置100个散点代表真实的污染物排放源。有没有获取每个点高度的方法?

这是我的代码:

%matplotlib inline

fig=plt.figure(figsize=(16,16))
ax=plt.subplot()
xi,yi = np.linspace(195.2260,391.2260,50),          
np.linspace(4108.9341,4304.9341,50)
height=np.array(list(csv.reader(open("/Users/HYF/Documents/SJZ_vis/Concentration/work/terr_grd.csv","rb"),delimiter=','))).astype('float')
cmap = cm.get_cmap(name='terrain', lut=None)
terrf = plt.contourf(xi, yi, height,100, cmap=cmap)
terr = plt.contour(xi, yi, height, 100,
             colors='k',alpha=0.5
             )
plt.clabel(terr, fontsize=7, inline=20)
ax.autoscale(False)
point= plt.scatter(dat_so2["xp"], dat_so2["yp"], marker='o',c="grey",s=40)


ax.autoscale(False)
for i in range(0,len(dat_so2["xp"]),1):
    plt.text(dat_so2["xp"][i], dat_so2["yp"][i],     
    str(i),color="White",fontsize=16)

ax.set_xlim(225,275)
ax.set_ylim(4200,4260)

plt.show()

【问题讨论】:

    标签: python-2.7 matplotlib


    【解决方案1】:

    您可以通过scipy.interpolate.interp2d 做到这一点

    例如,您可以在代码中添加:

    from scipy import interpolate
    hfunc = interpolate.interp2d(xi,yi,height)
    
    pointheights = np.zeros(dat_so2["xp"].shape)
    for i,(x,y) in enumerate(zip(dat_so2["xp"],dat_so2["yp"])):
        pointheights[i]=hfunc(x,y)
    

    将它与脚本的其余部分以及一些示例数据放在一起,就可以得到这个(我在这里简化了几件事,但你明白了):

    import matplotlib.pyplot as plt
    import matplotlib.cm as cm
    import numpy as np
    from scipy import interpolate
    
    fig=plt.figure(figsize=(8,8))
    ax=plt.subplot()
    #xi,yi = np.linspace(195.2260,391.2260,50),np.linspace(4108.9341,4304.9341,50)
    xi,yi = np.linspace(225,275,50),np.linspace(4200,4260,50)
    
    # A made up function of height (in place of your data)
    XI,YI = np.meshgrid(xi,yi)
    height = (XI-230.)**2 + (YI-4220.)**2
    #height=np.array(list(csv.reader(open("/Users/HYF/Documents/SJZ_vis/Concentration/work/terr_grd.csv","rb"),delimiter=','))).astype('float')
    
    cmap = cm.get_cmap(name='terrain', lut=None)
    terrf = plt.contourf(xi, yi, height,10, cmap=cmap)
    terr = plt.contour(xi, yi, height, 10,
                 colors='k',alpha=0.5
                 )
    plt.clabel(terr, fontsize=7, inline=20)
    ax.autoscale(False)
    
    # Some made up sample points
    dat_so2 = np.array([(230,4210),(240,4220),(250,4230),(260,4240),(270,4250)],dtype=[("xp","f4"),("yp","f4")]) 
    
    point= plt.scatter(dat_so2["xp"], dat_so2["yp"], marker='o',c="grey",s=40)
    
    # The interpolation function
    hfunc = interpolate.interp2d(xi,yi,height)
    
    # Now, for each point, lets interpolate the height
    pointheights = np.zeros(dat_so2["xp"].shape)
    for i,(x,y) in enumerate(zip(dat_so2["xp"],dat_so2["yp"])):
        pointheights[i]=hfunc(x,y)
    print pointheights
    
    
    ax.autoscale(False)
    for i in range(0,len(dat_so2["xp"]),1):
        plt.text(dat_so2["xp"][i], dat_so2["yp"][i],     
                 str(i),color="White",fontsize=16)
        # We can also add a height label to the plot
        plt.text(dat_so2["xp"][i], dat_so2["yp"][i],     
                 "{:4.1f}".format(pointheights[i]),color="black",fontsize=16,ha='right',va='top')
    
    
    ax.set_xlim(225,275)
    ax.set_ylim(4200,4260)
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2020-06-12
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多