Python Forum
IndexError: list index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: list index out of range
#1
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,


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.")
Reply


Messages In This Thread
IndexError: list index out of range - by rf_kartal - Sep-07-2021, 08:12 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 1,956 Sep-14-2023, 06:54 AM
Last Post: Mehboob
Thumbs Down I hate "List index out of range" Melen 20 3,161 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,979 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,847 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,280 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,411 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,505 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,112 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Python Error List Index Out of Range abhi1vaishnav 3 2,239 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 2,186 Jun-22-2021, 10:47 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020