-1

I am new in Python (and in programming at all) and trying to build the program for .csv data analysis.
I have multiple .csv files and for each of them I build a plot.
How do I add the name of the corresponding file to the title of the plot? Thanks!

Below is the coresponding part of the code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import glob

for fName in glob.glob('*.csv'): 
    df, loops = openF(fName)
    fig, ax = plt.subplots(1, 1)

    for i in range(loops):
        df.loc[df['loop']==i+1][['freq','phase']].plot(x='freq', y='phase', logx=True, ax=ax)
    ax.set_title('Phase/Freq'.format(fName))
    ax.set_xlabel('Frequency, [Hz]')
    ax.set_ylabel('Phase, [rad]')



plt.show()
David Leon
  • 1,017
  • 8
  • 25
Katya
  • 1
  • 2

1 Answers1

0

You've got it almost right.

In order for format method to work, you have to show it where to put the fName. So you have to change

ax.set_title('Phase/Freq'.format(fName))

to

ax.set_title('Phase/Freq {}'.format(fName)).

here is full documentation about format: https://docs.python.org/3.4/library/string.html#format-specification-mini-language

Also if you are happy enough to be using python3.6 you can use f-string https://www.python.org/dev/peps/pep-0498/

Quba
  • 4,776
  • 7
  • 34
  • 60