簡體   English   中英

在python上繪制3D數據

[英]Plotting 3D data on python

我是python編程的新手,我正在開發一個應用程序,該應用程序可以從3個列中繪制3D表面或輪廓圖,這些列是從文本文件中獲取的,並以逗號分隔。 我從使用地理坐標的3D圖中獲得了提示,但我不斷收到IndexError:列表索引超出范圍錯誤。 這是我的代碼:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import sys

ID = []
lat_col = []
long_col = []
Bouguer_col = []
cluster1 = 'Data.txt'
with open(cluster1) as f:
    lines = f.readlines()
    for line in lines:
        items = line.strip().split()
        lat = float(items[1])
        lon = float(items[2])
        bga = float(items[3])
        Bouguer_Anomaly = float(items[4])
        lat_col.append(lat)
        long_col.append(lon)
        Bouguer_col.append(bga)
        ID.append(Bouguer_Anomaly)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
p = ax.scatter(long_col, lat_col, Bouguer_col, c=ID, marker='o')

ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_zlabel('Bouguer Anomaly')
ax.invert__zaxis()
cb = fig.colorbar(p, label='Difference')
plt.savefig('plot.png')
plot.show()

如果有人可以幫助我提供從電子表格的各個列中繪制x,y,z數據的代碼,我也將感到高興!

.txt文件的部分內容如下所示:

Lat, Long, Elev, ObsGrav, Anomalies
6.671482000000001022e+00,7.372505999999999560e+00,3.612977999999999952e+02,9.780274000000000233e+05,-1.484474523360840976e+02
6.093078000000000216e+00,7.480882000000001142e+00,1.599972999999999956e+02,9.780334000000000233e+05,-1.492942383352201432e+02
6.092045999999999850e+00,7.278669999999999973e+00,1.462445999999999913e+02,9.780663000000000466e+05,-1.190960417173337191e+02
6.402087429999999912e+00,7.393360939999999992e+00,5.237939999999999827e+02,9.780468000000000466e+05,-8.033459449396468699e+01
6.264082730000000154e+00,7.518244540000000420e+00,2.990849999999999795e+02,9.780529000000000233e+05,-1.114865156192099676e+02
6.092975000000000030e+00,7.482914000000000065e+00,1.416474000000000046e+02,9.780338000000000466e+05,-1.525697779102483764e+02
6.383570999999999884e+00,7.289616999999999791e+00,2.590403000000000020e+02,9.780963000000000466e+05,-8.300666170357726514e+01
6.318417000000000172e+00,7.557638000000000744e+00,1.672036999999999978e+02,9.780693000000000466e+05,-1.246774551668204367e+02
6.253779999999999895e+00,7.268805999999999656e+00,1.059429999999999978e+02,9.781026999999999534e+05,-9.986763240839354694e+01
6.384635000000000282e+00,7.291032000000000401e+00,2.615624000000000251e+02,9.780963000000000466e+05,-8.256190758384764194e+01

我需要在3D表面或彩色輪廓圖上繪制[緯度,經度和異常]。

當您觀察到.csv文件具有標頭時,因此無法將其轉換為浮動標頭,適當的做法是跳過第一行,然后還有其他印刷錯誤,我已在以下代碼中糾正了這些錯誤:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import sys

ID = []
lat_col = []
long_col = []
Bouguer_col = []
cluster1 = 'data.txt'
with open(cluster1) as f:
    lines = f.readlines()
    for line in lines[1:]:
        lat, lon, elev, bga, Bouguer_Anomaly = [float(i) for i in line.strip().split(",")]
        lat_col.append(lat)
        long_col.append(lon)
        Bouguer_col.append(bga)
        ID.append(Bouguer_Anomaly)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
p = ax.scatter(long_col, lat_col, Bouguer_col, c=ID, marker='o')

ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_zlabel('Bouguer Anomaly')
ax.invert_zaxis()
cb = fig.colorbar(p, label='Difference')
plt.savefig('plot.png')
plt.show()

在此處輸入圖片說明

暫無
暫無

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

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