2017-11-28 4 views
1

저는 파이썬 프로그래밍에 익숙하지 않고 쉼표로 구분 된 값을 가진 텍스트 파일에서 얻은 3 열의 3D 표면 또는 등고선 그래프를 그릴 수있는 응용 프로그램을 만들고 있습니다. 나는 3D plot using geographic coordinates에서 힌트를 얻었지만 IndexError : 목록 인덱스가 범위를 벗어났습니다 오류. 이 내 코드입니다 :파이썬에서 3D 데이터 플로팅

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 

나는 3 차원 표면 또는 컬러 윤곽선 그래프 [위도, 긴 및 이상 현상을] 플롯 할 필요가있다.

+0

가 '항목 [1]'의 하나를 액세스 할 때, 인덱스가 존재하는지 확인 '항목 [2]', '항목 [3]'또는'항목 [4]'. 존재하지 않으면 적절한 조치를 취하십시오. – CupOfTea696

+0

.csv 파일을 보거나 공유 할 수 있습니다. – eyllanesc

+0

내 대답을 시도하십시오. – eyllanesc

답변

0

.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() 

enter image description here

+0

정말 기쁩니다. 고마워. 이 동일한 코드가 3D 표면/메쉬 플롯 및/또는 컬러 등고선 그래프에 유용할까요? 다시 한번 감사드립니다. –

+0

도움말 @eyllanesc !!! ** 표면 ** 또는 메쉬 플롯에서 작동하지 않을 수 있습니다 ** 색상 컨투어에서 작동하지 않습니다 ** –

+0

@DaramolaOlatoye 각 기능에는 특정 유형의 매개 변수가 필요합니다. 범용 응답이 없습니다 단일 응답 stackoverflow 존재하지 않습니다. – eyllanesc

관련 문제