цикл по многим файлам и построить их - PullRequest
0 голосов
/ 08 декабря 2018

Спасибо за потраченное время на чтение, возможно, это простой вопрос.У меня есть такой файл (они похожи на 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 кадров в секунду) только на одной фигуре.

в @GlobalTraveler, вот результат, используя ваше предложение:

ФАС Konoomachi_smooth_data

1 Ответ

0 голосов
/ 08 декабря 2018

Добавьте аргумент блока, чтобы показать -> plt.show (block = False) или переместите шоу за пределы цикла for

Однако, в общих чертах, я бы предложил переместить код в более OO-подход.Например:

#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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...