1

I have multiple text files with specific filename format in a directory, I want to concatenate all the content from all the files to a single .csv file and need to make an interactive 3D scatter plot using the specific data columns from the final CSV file. For this, I tried to concatenate the file's data into one. But my output has around 5000 entries instead of five hundred(after the 500 entries, the values repeating itself). Help me to find the error.

[Interactive plot : Able to zoom in / zoom out/ rotate the plot using mouse]

import fnmatch
import pandas as pd

data = pd.DataFrame()
for f_name in os.listdir(os.getcwd()):
 if fnmatch.fnmatch(f_name, 'hypoDD.reloc.*'):
     print(f_name)
     df=pd.read_csv(f_name,header=None,sep="\s+|\t")
     data=data.append(df,ignore_index=True)
     #print(data)


data.to_csv('outfile.txt',index=False)

OR

I want to make an interactive single 3D scatter plot using specific data columns from each file, and each file's data should be represented by different scatter color. ( I have ~18 different files and I don't even know 18 different colour names!)

VGB
  • 447
  • 6
  • 21

1 Answers1

0

Finally, I am able to write the code, even though the figure needs some more modifications like ( Put axis limits, reduce the scatter size, give scatter colour according to each file, Z-axis direction should be downward)

Suggestions?

import os
import glob

mypath = os.getcwd()
file_count = len(glob.glob1(mypath,"hypoDD.reloc.*"))
print("Number of clusters is:" ,file_count)

# Get .txt files

import fnmatch
import pandas as pd

data = pd.DataFrame()
for f_name in os.listdir(os.getcwd()):
 if fnmatch.fnmatch(f_name, 'hypoDD.reloc.*'):
     print(f_name)
     df=pd.read_csv(f_name,header=None,sep="\s+|\t")
     data=data.append(df,ignore_index=True)
     #print(data)


data.to_csv('outfile.txt',index=False)
latitude=data.iloc[:,1]
longitude=data.iloc[:,2]
depth=data.iloc[:,3]

scatter_data = pd.concat([longitude, latitude,depth], axis=1)
scatter_data.columns=['lon','lat','depth']

#------------------------------3D scatter--------------------------------
#----setting default renderer------------
import plotly.io as pio
pio.rrenderers
pio.renderers.default = "browser"
#-----------------------------------------
import plotly.express as px

fig = px.scatter_3d(scatter_data,x='lon', y='lat', z='depth')
fig.show()
fig.write_image("fig1.jpg")
VGB
  • 447
  • 6
  • 21