python - 通过连接多个文件创建的文本文件的交互式 3D 散点图

标签 python pandas text-files plotly scatter3d

我在一个目录中有多个具有特定文件名格式的文本文件,我想将所有文件中的所有内容连接到一个 .csv 文件,并且需要使用最终的特定数据列制作交互式 3D 散点图CSV 文件。为此,我尝试将文件的数据连接成一个。但我的输出有大约 5000 个条目,而不是 500 个(在 500 个条目之后,值会重复)。帮我找出错误所在。

[交互式绘图:可以使用鼠标放大/缩小/旋转绘图]

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)

或者

我想使用每个文件中的特定数据列制作交互式单个 3D 散点图,并且每个文件的数据应由不同的散点颜色表示。 (我有大约 18 个不同的文件,但我什至不知道 18 个不同的颜色名称!)

最佳答案

最后,我能够编写代码,即使该图需要更多修改,例如(放置轴限制,减小散点大小,根据每个文件给出散点颜色,Z轴方向应该向下)

建议?

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

关于python - 通过连接多个文件创建的文本文件的交互式 3D 散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59450986/

相关文章:

Python 找到 glib,但找不到 _glib

python - 了解 pandas merge 中的 "left_index"和 "right_index"参数

python - 有没有一种方法可以从两边都有不同数量的零包围的字符串中提取数字?

c++ - C++初学者:为什么我的编译器根据我的循环返回 “name not found”?

java从文本文件中读取元音

python - Pandas 向前填充具有特定值的时间戳列(1 秒)

python - 将数组中的零替换为连续的整数序列

python - 与 scipy.optimize.curve_fit 一起使用时迭代 python numpy 数组

python - 找不到资源错误: When Reading CSV from Azure Blob using Pandas with its SAS URL

java - 如何将数组列表的内容写入文本文件?