Posts: 10
Threads: 4
Joined: Sep 2021
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.")
Posts: 12,036
Threads: 486
Joined: Sep 2016
Would you please supply a sample '01011900-30092016_AllData.txt' file.
Posts: 10
Threads: 4
Joined: Sep 2021
(Sep-07-2021, 08:30 AM)Larz60+ Wrote: Would you please supply a sample '01011900-30092016_AllData.txt' file.
Posts: 12,036
Threads: 486
Joined: Sep 2016
since you only use pandas to read the file, perhaps a better solution would be:
from pathlib import Path
import csv
from CreateDict import CreateDict
import os
class DataPlot:
def __init__(self):
# Set starting directory same as code
os.chdir(os.path.abspath(os.path.dirname(__file__)))
self.cd = CreateDict()
homepath = Path('.')
self.infile = homepath / '01011900-30092016_AllData.txt'
def dispatch(self):
data = self.read_data()
def read_data(self):
with self.infile.open() as fp:
crdr = csv.DictReader(fp, delimiter=';')
for n, row in enumerate(crdr):
# Replace print statement with your code for each row
print(f"\n{n}: {self.cd.display_dict(row)}")
def main():
dp = DataPlot()
dp.dispatch()
if __name__ == '__main__':
main() This will read each row of the csv file into a dictionary format like:
Output: AgencyID: Ambraseys-Finkel
Time: 18.01.1900 15:30:00
Day: 18
Month: 01
Year: 1900
Hour: 15
Minute: 30
Second: 0
MiliSecond: 0
Latitude: 37.7900
Longitude: 28.2100
Depth: -10.0
Fixed: *
Mtype: MS
Magnitude: 4.40
M0:
Rms:
Herr:
Verr:
Gap:
StationNO:
Country: TÜRKÝYE
City: AYDIN
District: Yenipazar
Town:
Other:
EventType: Eq
you will need CreateDict.py:
import os
class CreateDict:
"""
Generic Software tools used by Trailmapper.
CreateDict.py - Contains methods to simplify node and cell creation within
a dictionary
Usage:
The best way to learn what can be done is to examine the testit function
included in this module.
new_dict(dictname) - Creates a new dictionary instance with the name
contained in dictname
add_node(parent, nodename) - Creates a new node (nested dictionary)
named in nodename, in parent dictionary.
add_cell(nodename, cellname, value) - Creates a leaf node within node
named in nodename, with a cell name of cellname, and value of value.
display_dict(dictname) - Recursively displays a nested dictionary.
Requirements:
Trailmapper software:
None
Python standard library:
os
Author: Larz60+ -- May 2019.
"""
def __init__(self):
os.chdir(os.path.abspath(os.path.dirname(__file__)))
def new_dict(self, dictname):
setattr(self, dictname, {})
def add_node(self, parent, nodename):
node = parent[nodename] = {}
return node
def add_cell(self, nodename, cellname, value):
cell = nodename[cellname] = value
return cell
def display_dict(self, dictname, level=0):
indent = " " * (4 * level)
for key, value in dictname.items():
if isinstance(value, dict):
print(f'\n{indent}{key}')
level += 1
self.display_dict(value, level)
else:
print(f'{indent}{key}: {value}')
if level > 0:
level -= 1
def testit():
# instantiate class
cd = CreateDict()
# create new dictionary named CityList
cd.new_dict('CityList')
# add node Boston
boston = cd.add_node(cd.CityList, 'Boston')
# add sub node Resturants
bos_resturants = cd.add_node(boston, 'Resturants')
# Add subnode 'Spoke Wine Bar' to parent bos_resturants
spoke = cd.add_node(bos_resturants, 'Spoke Wine Bar')
cd.add_cell(spoke, 'Addr1', '89 Holland St')
cd.add_cell(spoke, 'City', 'Sommerville')
cd.add_cell(spoke, 'Addr1', '02144')
cd.add_cell(spoke, 'Phone', '617-718-9463')
# Add subnode 'Highland Kitchen' to parent bos_resturants
highland = cd.add_node(bos_resturants, 'Highland Kitchen')
cd.add_cell(highland, 'Addr1', '150 Highland Ave')
cd.add_cell(highland, 'City', 'Sommerville')
cd.add_cell(highland, 'ZipCode', '02144')
cd.add_cell(highland, 'Phone', '617-625-1131')
# display dictionary
print(f'\nCityList Dictionary')
cd.display_dict(cd.CityList)
print(f'\nraw data: {cd.CityList}')
if __name__ == '__main__':
testit()
Posts: 10
Threads: 4
Joined: Sep 2021
(Sep-07-2021, 11:25 AM)Larz60+ Wrote: since you only use pandas to read the file, perhaps a better solution would be:
from pathlib import Path
import csv
from CreateDict import CreateDict
import os
class DataPlot:
def __init__(self):
# Set starting directory same as code
os.chdir(os.path.abspath(os.path.dirname(__file__)))
self.cd = CreateDict()
homepath = Path('.')
self.infile = homepath / '01011900-30092016_AllData.txt'
def dispatch(self):
data = self.read_data()
def read_data(self):
with self.infile.open() as fp:
crdr = csv.DictReader(fp, delimiter=';')
for n, row in enumerate(crdr):
# Replace print statement with your code for each row
print(f"\n{n}: {self.cd.display_dict(row)}")
def main():
dp = DataPlot()
dp.dispatch()
if __name__ == '__main__':
main() This will read each row of the csv file into a dictionary format like:
Output: AgencyID: Ambraseys-Finkel
Time: 18.01.1900 15:30:00
Day: 18
Month: 01
Year: 1900
Hour: 15
Minute: 30
Second: 0
MiliSecond: 0
Latitude: 37.7900
Longitude: 28.2100
Depth: -10.0
Fixed: *
Mtype: MS
Magnitude: 4.40
M0:
Rms:
Herr:
Verr:
Gap:
StationNO:
Country: TÜRKÝYE
City: AYDIN
District: Yenipazar
Town:
Other:
EventType: Eq
you will need CreateDict.py:
import os
class CreateDict:
"""
Generic Software tools used by Trailmapper.
CreateDict.py - Contains methods to simplify node and cell creation within
a dictionary
Usage:
The best way to learn what can be done is to examine the testit function
included in this module.
new_dict(dictname) - Creates a new dictionary instance with the name
contained in dictname
add_node(parent, nodename) - Creates a new node (nested dictionary)
named in nodename, in parent dictionary.
add_cell(nodename, cellname, value) - Creates a leaf node within node
named in nodename, with a cell name of cellname, and value of value.
display_dict(dictname) - Recursively displays a nested dictionary.
Requirements:
Trailmapper software:
None
Python standard library:
os
Author: Larz60+ -- May 2019.
"""
def __init__(self):
os.chdir(os.path.abspath(os.path.dirname(__file__)))
def new_dict(self, dictname):
setattr(self, dictname, {})
def add_node(self, parent, nodename):
node = parent[nodename] = {}
return node
def add_cell(self, nodename, cellname, value):
cell = nodename[cellname] = value
return cell
def display_dict(self, dictname, level=0):
indent = " " * (4 * level)
for key, value in dictname.items():
if isinstance(value, dict):
print(f'\n{indent}{key}')
level += 1
self.display_dict(value, level)
else:
print(f'{indent}{key}: {value}')
if level > 0:
level -= 1
def testit():
# instantiate class
cd = CreateDict()
# create new dictionary named CityList
cd.new_dict('CityList')
# add node Boston
boston = cd.add_node(cd.CityList, 'Boston')
# add sub node Resturants
bos_resturants = cd.add_node(boston, 'Resturants')
# Add subnode 'Spoke Wine Bar' to parent bos_resturants
spoke = cd.add_node(bos_resturants, 'Spoke Wine Bar')
cd.add_cell(spoke, 'Addr1', '89 Holland St')
cd.add_cell(spoke, 'City', 'Sommerville')
cd.add_cell(spoke, 'Addr1', '02144')
cd.add_cell(spoke, 'Phone', '617-718-9463')
# Add subnode 'Highland Kitchen' to parent bos_resturants
highland = cd.add_node(bos_resturants, 'Highland Kitchen')
cd.add_cell(highland, 'Addr1', '150 Highland Ave')
cd.add_cell(highland, 'City', 'Sommerville')
cd.add_cell(highland, 'ZipCode', '02144')
cd.add_cell(highland, 'Phone', '617-625-1131')
# display dictionary
print(f'\nCityList Dictionary')
cd.display_dict(cd.CityList)
print(f'\nraw data: {cd.CityList}')
if __name__ == '__main__':
testit()
Hi,
Thank you very much for your quick replay.
But, unless I misunderstood your answer, the problem is not related to reading the .csv file. There is a problem with drawing 3D graphics.
Posts: 10
Threads: 4
Joined: Sep 2021
(Sep-07-2021, 12:50 PM)rf_kartal Wrote: (Sep-07-2021, 11:25 AM)Larz60+ Wrote: since you only use pandas to read the file, perhaps a better solution would be:
from pathlib import Path
import csv
from CreateDict import CreateDict
import os
class DataPlot:
def __init__(self):
# Set starting directory same as code
os.chdir(os.path.abspath(os.path.dirname(__file__)))
self.cd = CreateDict()
homepath = Path('.')
self.infile = homepath / '01011900-30092016_AllData.txt'
def dispatch(self):
data = self.read_data()
def read_data(self):
with self.infile.open() as fp:
crdr = csv.DictReader(fp, delimiter=';')
for n, row in enumerate(crdr):
# Replace print statement with your code for each row
print(f"\n{n}: {self.cd.display_dict(row)}")
def main():
dp = DataPlot()
dp.dispatch()
if __name__ == '__main__':
main() This will read each row of the csv file into a dictionary format like:
Output: AgencyID: Ambraseys-Finkel
Time: 18.01.1900 15:30:00
Day: 18
Month: 01
Year: 1900
Hour: 15
Minute: 30
Second: 0
MiliSecond: 0
Latitude: 37.7900
Longitude: 28.2100
Depth: -10.0
Fixed: *
Mtype: MS
Magnitude: 4.40
M0:
Rms:
Herr:
Verr:
Gap:
StationNO:
Country: TÜRKÝYE
City: AYDIN
District: Yenipazar
Town:
Other:
EventType: Eq
you will need CreateDict.py:
import os
class CreateDict:
"""
Generic Software tools used by Trailmapper.
CreateDict.py - Contains methods to simplify node and cell creation within
a dictionary
Usage:
The best way to learn what can be done is to examine the testit function
included in this module.
new_dict(dictname) - Creates a new dictionary instance with the name
contained in dictname
add_node(parent, nodename) - Creates a new node (nested dictionary)
named in nodename, in parent dictionary.
add_cell(nodename, cellname, value) - Creates a leaf node within node
named in nodename, with a cell name of cellname, and value of value.
display_dict(dictname) - Recursively displays a nested dictionary.
Requirements:
Trailmapper software:
None
Python standard library:
os
Author: Larz60+ -- May 2019.
"""
def __init__(self):
os.chdir(os.path.abspath(os.path.dirname(__file__)))
def new_dict(self, dictname):
setattr(self, dictname, {})
def add_node(self, parent, nodename):
node = parent[nodename] = {}
return node
def add_cell(self, nodename, cellname, value):
cell = nodename[cellname] = value
return cell
def display_dict(self, dictname, level=0):
indent = " " * (4 * level)
for key, value in dictname.items():
if isinstance(value, dict):
print(f'\n{indent}{key}')
level += 1
self.display_dict(value, level)
else:
print(f'{indent}{key}: {value}')
if level > 0:
level -= 1
def testit():
# instantiate class
cd = CreateDict()
# create new dictionary named CityList
cd.new_dict('CityList')
# add node Boston
boston = cd.add_node(cd.CityList, 'Boston')
# add sub node Resturants
bos_resturants = cd.add_node(boston, 'Resturants')
# Add subnode 'Spoke Wine Bar' to parent bos_resturants
spoke = cd.add_node(bos_resturants, 'Spoke Wine Bar')
cd.add_cell(spoke, 'Addr1', '89 Holland St')
cd.add_cell(spoke, 'City', 'Sommerville')
cd.add_cell(spoke, 'Addr1', '02144')
cd.add_cell(spoke, 'Phone', '617-718-9463')
# Add subnode 'Highland Kitchen' to parent bos_resturants
highland = cd.add_node(bos_resturants, 'Highland Kitchen')
cd.add_cell(highland, 'Addr1', '150 Highland Ave')
cd.add_cell(highland, 'City', 'Sommerville')
cd.add_cell(highland, 'ZipCode', '02144')
cd.add_cell(highland, 'Phone', '617-625-1131')
# display dictionary
print(f'\nCityList Dictionary')
cd.display_dict(cd.CityList)
print(f'\nraw data: {cd.CityList}')
if __name__ == '__main__':
testit()
Hi,
Thank you very much for your quick replay.
But, unless I misunderstood your answer, the problem is not related to reading the .csv file. There is a problem with drawing 3D graphics.
Hi again,
I solved the problem.
Before code:
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) After code:
for i in range(len(all_data)):
if all_data[i][3] < 3.0:
color = 'grey'
marker_size = 1 * all_data[i][3]
label = '$0 < M < 3.0$'
elif 3.0 <= all_data[i][3] < 4.0:
color = 'blue'
marker_size = 2 * all_data[i][3]
label = '$3.0 <= M < 4.0$'
elif 4.0 <= all_data[i][3] < 5.0:
color = 'black'
marker_size = 3 * all_data[i][3]
label = '$4.0 <= M < 5.0$'
elif 5.0 <= all_data[i][3] < 6.0:
color = 'limegreen'
marker_size = 4 * all_data[i][3]
label = '$5.0 <= M < 6.0$'
elif 6.0 <= all_data[i][3] < 7.0:
color = 'lightcoral'
marker_size = 5 * all_data[i][3]
label = '$6.0 <= M < 7.0$'
elif all_data[i][3] >= 7.0:
color = 'red'
marker_size = 7 * all_data[i][3]
label = '$M >= 7.0$'
ax.scatter(all_data[i][0], all_data[i][1], all_data[i][2], marker='o', color=color, s=marker_size)
Posts: 12,036
Threads: 486
Joined: Sep 2016
Glad you solved your problem. I knew that reading the file was not the issue, I was just showing a different way to do so.
Thanks for sharing your solution.
|