Sep-07-2021, 08:12 AM
Hi,
Below is the code that worked before, it gives the error "list index out of range".
I don't know where is the wrong. Could you please help me?
Best regards,
Below is the code that worked before, it gives the error "list index out of range".
I don't know where is the wrong. Could you please help me?
Best regards,
import numpy as np from matplotlib import cbook from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import os.path import pandas # fileName = input("This program is designed to draw 3D graphics.\nThe file structure must have Latitude, Longitude, Depth, and Magnitude columns.\nPlease enter the file name (e.g '/path/to/file.name') :") fileName = 'D:/PYTHON STUDIES/Seismic_Data_Analysis/01011900-30092016_AllData.txt' name, extension = os.path.splitext(fileName) def fileExtension(extension): return { '.txt': 1, '.csv': 2, '.xls': 3, '.xlsx': 4 }[extension] def plot_3D_graph(all_data): ax = plt.figure(figsize=(10, 5)).gca(projection='3d') for i in range(5, len(all_data)): if all_data[0:3][i][3] < 3.0: color = 'grey' marker_size = 1 * all_data[0:3][i][3] label = '$0 < M < 3.0$' elif 3.0 <= all_data[0:3][i][3] < 4.0: color = 'blue' marker_size = 2 * all_data[0:3][i][3] label = '$3.0 <= M < 4.0$' elif 4.0 <= all_data[0:3][i][3] < 5.0: color = 'black' marker_size = 3 * all_data[0:3][i][3] label = '$4.0 <= M < 5.0$' elif 5.0 <= all_data[0:3][i][3] < 6.0: color = 'limegreen' marker_size = 4 * all_data[0:3][i][3] label = '$5.0 <= M < 6.0$' elif 6.0 <= all_data[0:3][i][3] < 7.0: color = 'lightcoral' marker_size = 5 * all_data[0:3][i][3] label = '$6.0 <= M < 7.0$' elif all_data[0:3][i][3] >= 7.0: color = 'red' marker_size = 7 * all_data[0:3][i][3] label = '$M >= 7.0$' ax.scatter(all_data[0:3][i][0], all_data[0:3][i][1], all_data[0:3][i][2], marker='o', label=label, color=color, s=marker_size) ax.set_xlabel('Latitude') ax.set_ylabel('Longitude') ax.set_zlabel('Depth(Km)') plt.legend(loc='upper left') plt.show() def read_txt_csv_file(fileName): all = pandas.read_csv(fileName, usecols=["Latitude", "Longitude", "Depth", "Magnitude"], encoding='latin-1', delimiter=';').values all_data = sorted(all, key=lambda x: float(x[3])) # sorted by Magnitude column ................ plot_3D_graph(all_data) def read_xls_file(fileName): pass def read_xlsx_file(fileName): pass if fileExtension(extension) == 1: read_txt_csv_file(fileName) elif fileExtension(extension) == 2: read_txt_csv_file(fileName) elif fileExtension(extension) == 3: read_xls_file(fileName) elif fileExtension(extension) == 4: read_xlsx_file(fileName) else: print("Your file is different from '.txt', '.csv', '.xls', '.xlsx' file types.")