簡體   English   中英

Matplotlib 輪廓不規則數據

[英]Matplotlib Contourf with Irregular Data

我有一個深度在不同 x 點的土壤特性數據。 鑽孔數據的深度或數量不相等,因此我必須標准化代碼。 如果所有鑽孔具有相同數量的數據和深度,沒問題,np.meshgrid 可以正常工作。 但是,就我而言,我遇到了麻煩,無法繪制輪廓 plot。

這是不可能的還是我做錯了什么?

input_data = {
    "BH1": {
        "Chainage": 50,
        "Depth": [2, 3, 4, 5, 6,7,10],
        "Parameter": [10, 5, 12, 56, 34,45,62],
    },
    "BH2": {"Chainage": 0, "Depth": [2, 3, 4, 5, 6, 18], "Parameter": [2, 4, 12, 23, 12, 33]},
    "BH3": {
        "Chainage": -50,
        "Depth": [2, 3, 4, 5, 6, 9],
        "Parameter": [12, 14, 22, 33, 32, 70],
    },
}

import numpy as np
import matplotlib.pyplot as plt

#PREPROCESSING OF DATA

depth_lengths = []
for i in input_data.keys():
    depth_lengths.append(len(input_data[i]["Depth"]))

max_depth_length = max(depth_lengths)

for i in input_data.keys():
    while len(input_data[i]["Depth"]) < max_depth_length:
        input_data[i]["Depth"].append(None)
        input_data[i]["Parameter"].append(None)

parameter = []

for i in range(max_depth_length):
    temp = []
    for j in input_data.keys():
        temp.append(input_data[j]["Parameter"][i])
    parameter.append(temp)    
    
        
depth = []
chainage = []
parameter2 = []
for i in input_data.keys():
    for j in input_data[i]["Depth"]:
        depth.append(j)
    for j in input_data[i]["Parameter"]:
        parameter2.append(j)
    chainage.append(input_data[i]["Chainage"])
        
# X, Y = np.meshgrid(chainage, depth)

parameter2 = np.array(parameter2*3).reshape(-1,3)


fig,ax=plt.subplots()
ax.contourf(X, Y, parameter2, 8, alpha=.75, cmap='jet')

不確定這是否是您想要的,但這會使用您的數據生成填充輪廓 plot。 z(參數)有很多缺失值,所以圖表看起來不太好。

X=     [[50,-50,-50,-50,-50,-50,-50,-50,-50],
        [0,   0,  0,  0,  0,  0, 0, 0, 0],
        [50, 50, 50, 50, 50, 50, 50, 50, 50]]
Y=     [[2, 3, 4, 5, 6, 7, 9, 10, 18],
        [2, 3, 4, 5, 6, 7, 9, 10, 18],
        [2, 3, 4, 5, 6, 7, 9, 10, 18]]
z= 3*np.array([[12, 14, 22, 33, 32, np.NaN, 70, np.NaN, np.NaN],
    [2, 4, 12, 23, 12, np.NaN, np.NaN, np.NaN, 33],
    [10, 5, 12, 56, 34,45,np.NaN,  62, np.NaN]])

plt.contourf(X, Y, z, 8, alpha=.75, cmap='jet')
plt.show()

對於未按規則網格組織的數據, tricontourf通過三角形連接輸入點來創建輪廓 plot。 您可以使用np.repeat創建長度與depth s 相同的chainage數列表(或一維數組)。 只需遍歷depth s 和parameter s 即可創建相應的列表。

import matplotlib.pyplot as plt
import numpy as np

input_data = {"BH1": {"Chainage": 50,
                      "Depth": [2, 3, 4, 5, 6, 7, 10],
                      "Parameter": [10, 5, 12, 56, 34, 45, 62]},
              "BH2": {"Chainage": 0,
                      "Depth": [2, 3, 4, 5, 6, 18],
                      "Parameter": [2, 4, 12, 23, 12, 33]},
              "BH3": {"Chainage": -50,
                      "Depth": [2, 3, 4, 5, 6, 9],
                      "Parameter": [12, 14, 22, 33, 32, 70]}}
chainage = np.repeat([v["Chainage"] for v in input_data.values()], [len(v["Depth"]) for v in input_data.values()])
depth = [d for v in input_data.values() for d in v["Depth"]]
parameter = [p for v in input_data.values() for p in v["Parameter"]]

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(16, 5))
ax1.tricontourf(chainage, depth, parameter, levels=8, alpha=.75, cmap='jet')
ax2.scatter(chainage, depth, c=parameter, cmap='jet')

plt.show()

右側的 plot 顯示輸入,顏色為散點 plot。 左邊的 plot 顯示了對應的輪廓 plot。

結果圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM