2

I have the following function to plot a graph:

def plot_ATD(DataFrame):
    #Initialise 225V ATD plot
    fig = plt.figure()
    ax = fig.add_subplot(111)
    #take columns from data set and make to list which is passed to matplotlib to plot a graph
    x = DataFrame['Arrival Time (ms)'].tolist()
    y = DataFrame['Intensity'].tolist()
    line, = ax.plot(x,y, 'r-')
    #use numpy to get the max of Intensity, then determine the corresponding arrival time
    ymax = np.max(y)
    xpos = y.index(ymax)
    xmax = x[xpos]
    time = xmax
    #add an annotation point at the maxima giving the arrival time at this position
    # ax.annotate(s=text of annotation, xy=point to annotate, xytext=position to place text
    #              arrowprops=dict(facecolor=color of arrow))
    ax.annotate(s=xmax, xy=(xmax, ymax), xytext=(xmax+5, ymax+5),
                arrowprops=dict(facecolor='orange'),
               )
    #ax.set_ylim(0,600000)
    ax.set_xlim(0,20)
    plt.xlabel('Arrival time (ms)')
    plt.title(DataFrame.name)
    return plt.show()

I am using it on the following pandas DataFrames:

V100 = pd.read_csv('Documents/spreadsheets/Data/100V_9z.csv', names=['Arrival Time (ms)', 'Intensity'])
V125 = pd.read_csv('Documents/spreadsheets/Data/125V_9z.csv', names=['Arrival Time (ms)', 'Intensity'])
V150 = pd.read_csv('Documents/spreadsheets/Data/150V_9z.csv', names=['Arrival Time (ms)', 'Intensity'])
V175 = pd.read_csv('Documents/spreadsheets/Data/175V_9z.csv', names=['Arrival Time (ms)', 'Intensity'])
V200 = pd.read_csv('Documents/spreadsheets/Data/200V_9z.csv', names=['Arrival Time (ms)', 'Intensity'])
V225 = pd.read_csv('Documents/spreadsheets/Data/225V_9z.csv', names=['Arrival Time (ms)', 'Intensity'])

I want to have the title of the graph to be the name of the DataFrame i.e. V100, V125 etc.

I am not sure on the right syntax or how to do this? Please help!

3
  • So plt.title(DataFrame.name) does not work? Commented Nov 20, 2018 at 7:11
  • @b-fg - What is DataFrame here Commented Nov 20, 2018 at 7:13
  • 1
    It's OPs variable name for the dataframe in the function. I have commented on this bad practice in my answer. Commented Nov 20, 2018 at 7:17

2 Answers 2

4

First, is not good practice to use DataFrame as the name of your dataframe in the function since it is the name of the pandas.DataFrame class itself. Better change it to df for example.

So you can set the name of the dataframe with (for example)

V100.name = 'V100'

And do this for all your dataframes. Then in your function call (the newly named) df.name to get the name you previously assigned to the dataframe.

Update

To automatically set the dataframe names you can simply do

file_name = 'Documents/spreadsheets/Data/100V_9z.csv'
V100 = pd.read_csv(file_name, names=['Arrival Time (ms)', 'Intensity'])
V100.name = file_name.split('/')[-1].split('_')[0] # 'V100'
Sign up to request clarification or add additional context in comments.

5 Comments

May I know what is advantage over assigning this value in to another value and access it again instead of df.name?
Is a way to pass it to the function tied with the dataframe itself. No need to create other variables when there is a specific class attribute for this.
Happy to contribute :)
thank you this seems to work, So I have created the variables V100.name = 'V100' etc for each one outside the function. Is there no way to have the function do it within, so I could rename my dataframes and not have to redo the df.name variables?
This is not the original question... You can, I will update my question now.
2

Here a workaround:

Vs= [v for v in locals() if v.startswith('V')] 
for v in Vs:
    plot(eval(v),title=v)

A cleaner method (since eval is not secure) must be done when creating variables, for example with series (or dictionaries) :

ser=pd.Series()
ser['V100'] = pd.read_csv('Documents/spreadsheets/Data/100V_9z.csv', \
names=['Arrival Time (ms)', 'Intensity'])

will simplify the job.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.