0

I try to plot 3 plots for each columns ('AFp1','AFp2','F9') in one figure with 'freqs' on the x axis and 'psd' on the y axis. I'm looking for a kind of loop through the variables because at the end I want to plot >50 plots in one figure.

Here I found a code that seems to do what I want but I don't get it to work:

num_plots = 20

colormap = plt.cm.gist_ncar
plt.gca().set_prop_cycle(plt.cycler('color', plt.cm.jet(np.linspace(0, 1, num_plots))))

x = np.arange(10)
labels = []
for i in range(1, num_plots + 1):
    plt.plot(x, i * x + 5 * i)
    labels.append(r'$y = %ix + %i$' % (i, 5*i))

plt.legend(labels, ncol=4, loc='upper center', 
           bbox_to_anchor=[0.5, 1.1], 
           columnspacing=1.0, labelspacing=0.0,
           handletextpad=0.0, handlelength=1.5,
           fancybox=True, shadow=True)

plt.show()

Here is how I tried to include this code in my for loop:

path = r'C:/M'
for fil in os.listdir(path):
    #extract SUBJECT name
    r = (fil.split(" ")[0])
    #load file in pandas dataframe
    data = pd.read_csv(path+f'{r} task.txt',sep=",",usecols= 'AFp1','AFp2','F9'])
    data.columns = ['AFp1','AFp2','F9']
    
   num_plots = 3
    for columns in data(1, num_plots + 1):
        freqs, psd = signal.welch(data[columns], fs=500,
                                  window='hanning',nperseg=1000, noverlap=500, scaling='density', average='mean')
        
        colormap = plt.cm.gist_ncar
        plt.gca().set_prop_cycle(plt.cycler('color', plt.cm.jet(np.linspace(0, 1, num_plots))))
        
        plt.plot(freqs, psd)
        plt.legend(columns, ncol=4, loc='upper center', 
                   bbox_to_anchor=[0.5, 1.1], 
                   columnspacing=1.0, labelspacing=0.0,
                   handletextpad=0.0, handlelength=1.5,
                   fancybox=True, shadow=True)
        plt.title(f'PSD for {r}')#, nperseg=1000, noverlap=500
        plt.xlabel('Frequency [Hz]')
        plt.ylabel('Power [V**2/Hz]')
        plt.axis([0,50, -1, 5])
               
        plt.show()

I get the following error:

   for columns in data(1, num_plots + 1):

TypeError: 'DataFrame' object is not callable 

If anyone could tell me how I can make it work, it would be great :D

Thank you very much, Angelika

Angelique
  • 11
  • 3
  • 1
    A reproductible example would be great – Rafael Valero Jan 31 '21 at 00:27
  • Hi Rafael, I edited my question. I think it is more precise now. I'm not sure what you mean by 'reproducible example'. Let me know if you need some more information. Thank you very much!!! – Angelique Feb 01 '21 at 09:44

2 Answers2

1

Shoaib's answer finally worked. Thank you very much: "you should only use plt.show() once, so put it outside of for loop. your error is because data is an array but you used it as a function like data(something). you should see what is dimensions of data and then try to select columns or values using data[ something ] not data( something ). check dimensions of data using codes like print(data) or print(data[0]) or print(len(data)) or print(len(data[0])) etc. it will help you in debugging your code "

Angelique
  • 11
  • 3
0

here is how you plot three functions in a figure

import matplotlib.pyplot as plt
from math import *

x_lim = 6
n = 1000
X = []
Y1 = []
Y2 = []
Y3 = []
for i in range(n):
    x = x_lim * (i/n-1)
    y1 = sin(x)
    y2 = cos(x)
    y3 = x**2
    X.append( x )
    Y1.append( y1 )
    Y2.append( y2 )
    Y3.append( y3 )
    
plt.plot(X,Y1)
plt.plot(X,Y2)
plt.plot(X,Y3)
plt.title("title")
plt.xlabel("x")
plt.ylabel("y")
plt.show()

your question was not reproducible, because the file you are getting your data from is not entailed. so we can not reproduce your error with copy paste your code. but if you have an array namely data with for example 4 columns then you can separate each columns then plot them

for row in data:
    x.append( row[0] )
    Y1.append( row[1] )
    Y2.append( row[2] )
    Y3.append( row[3] )
Shoaib Mirzaei
  • 512
  • 4
  • 11
  • Hi Shoaib, Thank you very much for your reply. I modified my post to try to make my question clearer. I'm looking for a code that kind of loops though the variables AFp1, AFp2 and F9 instead of adding a line per variable; as at the end I want to use >50 variables in one figure. – Angelique Feb 01 '21 at 14:11
  • you should only use `plt.show()` once, so put it outside of for loop. your error is because `data` is an array but you used it as a function like `data(something)`. you should see what is dimensions of `data` and then try to select columns or values using `data[ something ]` not `data( something )`. check dimensions of `data` using codes like `print(data)` or `print(data[0])` or `print(len(data))` or `print(len(data[0]))` etc. it will help you in debugging your code – Shoaib Mirzaei Feb 01 '21 at 18:57
  • That's it!!! Thank you very much Shoaib!!! Unfortunately, I think I cannot mark your reply as the correct answer! – Angelique Feb 02 '21 at 10:52