如何用线图限制轮廓图?

时间:2018-12-10 15:22:35

标签: python contour

我试图绘制此轮廓,直到#upperlimit坐标变为黑色,结果应该类似于图b(附加)figure

图A是下一个代码的输出:

from scipy.interpolate import griddata 
import numpy as np 
import matplotlib.pyplot as plt 


M = np.array([[0.000000,1217.000000,594.503284],
    [4.500000,1183.886353,2099.999905],
    [9.000000,1220.000000,1071.599126],
    [13.500000,1184.565430,2099.999905],
    [18.000000,1219.000000,435.631812],
    [22.500000,1185.150635,2099.999905],
    [24.500000,1217.555542,320.541441],
    [27.000000,1185.427490,2099.999905],
    [31.500000,1216.000000,300.000012],
    [36.000000,1185.981445,2099.999905],
    [40.500000,1215.000000,306.778669],
    [45.000000,1186.629272,2099.999905],
    [49.500000,1216.000000,300.000012],
    [54.000000,1187.214478,2099.999905],
    [58.500000,1215.000000,300.000012],
    [63.000000,1187.893555,2099.999905],
    [67.500000,1218.000000,335.902870],
    [72.000000,1188.572510,2099.999905],
    [76.500000,1220.000000,359.615386],
    [81.000000,1189.282715,2099.999905],
    [85.500000,1224.000000,1382.480264],
    [90.000000,1189.992920,2099.999905],
    [94.500000,1225.000000,1206.023455],
    [99.000000,1190.578125,2099.999905]])

    #upper,limit
h = np.array([[0.0,1217],
    [4.5,1217],
    [9.0,1220],
    [13.5,1219],
    [18.0,1219],
    [22.5,1218],
    [27.0,1217],
    [31.5,1216],
    [36.0,1215],
    [40.5,1215],
    [45.0,1216],
    [49.5,1216],
    [54.0,1215],
    [58.5,1215],
    [63.0,1217],
    [67.5,1218],
    [72.0,1219],
    [76.5,1220],
    [81.0,1222],
    [85.5,1224],
    [90.0,1225],
    [94.5,1225],
    [99.0,1224],
    [103.5,1225]])
##
    x=M[:,0] 
    y=M[:,1]
    z=M[:,2]
##  

    xi=np.linspace(0,100.,100.) 
    yi=np.linspace(1190,1225.,1225.) 
    X,Y= np.meshgrid(xi,yi) 
    Z = griddata((x, y), z, (X, Y),method='cubic') 
    plt.contourf(X,Y,Z)
    plt.colorbar()
    plt.plot(h[:,0],h[:,1],'black',linewidth=2)
    plt.scatter(x,y,marker ='o',c='k',s=10,zorder=10)
    plt.xlim(0,100)
    plt.ylim(1190,1225)
    plt.grid(True)
    plt.rc('grid',linestyle="-",color='black')
    plt.show()

想法是隐藏或删除黑线上方的轮廓图。我用一个补丁在Matlab上解决了这个问题,想知道用python解决它的最佳方法吗?

1 个答案:

答案 0 :(得分:0)

我不确定您是否可以使用现有功能来实现此目的。
您可以做的是对网格的所有点进行检查,如果y超过h所定义的极限,则将X,Y,Z值命名为:
(分配Z后添加这段代码)

x0 = 0
dx = 4.5
for n1, xv in enumerate( X):
    for n2, x in enumerate( xv):
        index = (x - x0) / dx
        i1, i0 = int(np.ceil( index)), int(np.floor( index))
        y = (h[i1][1] - h[i0][1]) / dx * (x - h[i0][0]) + h[i0][1]
        if Y[n1][n2] > y:
            X[n1][n2] = np.nan
            Y[n1][n2] = np.nan
            Z[n1][n2] = np.nan

编辑:添加了完整代码,以避免复制粘贴问题

from scipy.interpolate import griddata 
import numpy as np 
import matplotlib.pyplot as plt 


M = np.array([[0.000000,1217.000000,594.503284],
    [4.500000,1183.886353,2099.999905],
    [9.000000,1220.000000,1071.599126],
    [13.500000,1184.565430,2099.999905],
    [18.000000,1219.000000,435.631812],
    [22.500000,1185.150635,2099.999905],
    [24.500000,1217.555542,320.541441],
    [27.000000,1185.427490,2099.999905],
    [31.500000,1216.000000,300.000012],
    [36.000000,1185.981445,2099.999905],
    [40.500000,1215.000000,306.778669],
    [45.000000,1186.629272,2099.999905],
    [49.500000,1216.000000,300.000012],
    [54.000000,1187.214478,2099.999905],
    [58.500000,1215.000000,300.000012],
    [63.000000,1187.893555,2099.999905],
    [67.500000,1218.000000,335.902870],
    [72.000000,1188.572510,2099.999905],
    [76.500000,1220.000000,359.615386],
    [81.000000,1189.282715,2099.999905],
    [85.500000,1224.000000,1382.480264],
    [90.000000,1189.992920,2099.999905],
    [94.500000,1225.000000,1206.023455],
    [99.000000,1190.578125,2099.999905]])

    #upper,limit
h = np.array([[0.0,1217],
    [4.5,1217],
    [9.0,1220],
    [13.5,1219],
    [18.0,1219],
    [22.5,1218],
    [27.0,1217],
    [31.5,1216],
    [36.0,1215],
    [40.5,1215],
    [45.0,1216],
    [49.5,1216],
    [54.0,1215],
    [58.5,1215],
    [63.0,1217],
    [67.5,1218],
    [72.0,1219],
    [76.5,1220],
    [81.0,1222],
    [85.5,1224],
    [90.0,1225],
    [94.5,1225],
    [99.0,1224],
    [103.5,1225]])
##
x=M[:,0] 
y=M[:,1]
z=M[:,2]
##  

xi=np.linspace(0,100.,100.) 
yi=np.linspace(1190,1225.,1225.) 
X,Y= np.meshgrid(xi,yi) 
Z = griddata((x, y), z, (X, Y),method='cubic') 

x0 = 0
dx = 4.5
for n1, xv in enumerate( X):
    for n2, x in enumerate( xv):
        index = (x - x0) / dx
        i1, i0 = int(np.ceil( index)), int(np.floor( index))
        y = (h[i1][1] - h[i0][1]) / dx * (x - h[i0][0]) + h[i0][1]
        if Y[n1][n2] > y:
            X[n1][n2] = np.nan
            Y[n1][n2] = np.nan
            Z[n1][n2] = np.nan

plt.contourf(X,Y,Z)
plt.colorbar()
plt.plot(h[:,0],h[:,1],'black',linewidth=2)
plt.scatter(x,y,marker ='o',c='k',s=10,zorder=10)
plt.xlim(0,100)
plt.ylim(1190,1225)
plt.grid(True)
plt.rc('grid',linestyle="-",color='black')
plt.show()