-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()
2
  • You forgot {} in .format. But you could write ax.set_title('Phase/Freq %s'fName) for instance or simply ax.set_title('Phase/Freq ' + fName) instead. Commented Mar 21, 2018 at 8:03
  • Thanks, David Leon, it works now!!! :) Commented Mar 21, 2018 at 8:36

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.