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