【问题标题】:loop over many files and plot them遍历许多文件并绘制它们
【发布时间】:2018-12-08 20:32:53
【问题描述】:

感谢您花时间阅读它,也许这是一个简单的问题。 我有一个这样的文件(它们就像 200 个文件):

    Output of SMC2FS2: FAS for file 20123427.CB2A.BHE.sac.smc
 Nfreq_out = 
   8192
           freq            fas
  0.0000000E+00  6.6406252E-03
  2.4414062E-03  1.3868844E+04
  4.8828125E-03  3.0740834E+04
  7.3242188E-03  2.7857139E+04
  9.7656250E-03  1.6535047E+04
  1.2207031E-02  9.7825762E+03
  1.4648438E-02  6.1421987E+03
  1.7089844E-02  6.5783145E+03
  1.9531250E-02  5.6137949E+03
  2.1972656E-02  3.5297178E+03

要阅读它们,跳过标题并开始处理:

#define the path where I have the 200 files
pato='D:\\Seismic_Inves\\flc_grant\\120427\\smc2fs\\smooth'
os.chdir(pato)
lista=[]
#list all files with "kono_" 
for a in glob.glob('*kono_*'):
    lista.append(a)
#read and skip the header for all files
for archis in lista:
    with open(archis,'r') as leo:
       for _ in range(4):
            next(leo)
#start the proccesing
       for line in leo:
           leo=[x.strip() for x in leo if x.strip()]
           leos=[tuple(map(float,x.split())) for x in leo[1:]]
           f=[x[0] for x in leos]
           fas=[x[1] for x in leos]
           plt.figure(1)
           plt.plot(f,fas,'r')
           plt.yscale('log')
           plt.xscale('log')
           plt.show()

您可以想象它是频率与幅度的图(FAS 图) 该代码运行良好,但打开一个图形并仅绘制一个文件,然后我需要关闭该图形,它将绘制第二个文件,依此类推。

问题是:

如何在一张图中绘制所有数据(200 个 fcsv 文件)。

致@GlobalTraveler,这是使用您的建议的结果:

FAS Konoomachi_smooth_data

【问题讨论】:

  • 因为我没有数据,这就是你想要的是吗?
  • @GlobalTraveler 是的,这是我知道的,地震台站的 FAS 图及其行为,所有这些都使用 Konoomachi 平滑技术。
  • 不确定那是哪种平滑。无论如何,您可以将问题标记为已回答吗?如果您还有任何问题,请给我留言。
  • @GlobalTraveler,完成

标签: python loops csv


【解决方案1】:

将块参数添加到 show -> plt.show(block = False) 或将 show 移到 for 循环之外

但是,总体而言,我建议将代码移至更多面向对象的方法。例如:

#define the path where I have the 200 files
from matplotlib.pyplot import subplots, show
pato='D:\\Seismic_Inves\\flc_grant\\120427\\smc2fs\\smooth'
os.chdir(pato)
lista=[]
#list all files with "kono_" 
for a in glob.glob('*kono_*'):
    lista.append(a)
#read and skip the header for all files

fig, ax  = subplots() # open figure and create axis
for archis in lista:
    with open(archis,'r') as leo:
       for _ in range(4):
            next(leo)
#start the proccesing
       for line in leo:
           leo=[x.strip() for x in leo if x.strip()]
           leos=[tuple(map(float,x.split())) for x in leo[1:]]
           f=[x[0] for x in leos]
           fas=[x[1] for x in leos]
           ax.plot(f,fas,'r') # plot on this axis
ax.set(**dict(xscale = 'log', yscale = 'log')) # format the axis
show() # show

这是您的建议的结果 FAS_konoomachi_smooth

【讨论】:

  • 我同意 O.O.编程,最好是工作 subplots()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多