我怎样才能遍历绘图 matplotlib 的标题?
How can I loop through the title of a plot matplotlib?
我是 python 的新手,正在尝试通过 for 循环为我的 EEG 数据集的每个电极在单独的图中绘制 PSD。图的标题应包括相应的电极名称。
这是我用来从 .txt 文件加载数据的代码:
k = pd.read_csv(r'C:\Users\LPC\Desktop\rest txt 7min\AB24_rest_asr_ICA_MARA_7min.txt',usecols=['AFp2','F9','AFF5h','AFF1h','AFF2h','AFF6h','F10','FFT9h','FFT7h','FFC5h','FFC3h','FFC1h','FFC2h','FFC4h','FFC6h','FFT8h','FFT10h','FC1','FCz','FC2','FTT9h','FTT7h','FCC5h','FCC3h','FCC1h','FCC2h','FCC4h','FCC6h','FTT8h','FTT10h','Cz','TTP7h','CCP5h','CCP3h','CCP1h','CCP2h','CCP4h','CCP6h','TTP8h','CPz','TPP9h','TPP7h','CPP5h','CPP3h','CPP1h','CPP2h','CPP4h','CPP6h','TPP8h','TPP10h','Pz','PPO1h','PPO2h','P9','PPO9h','POO1','POO2','PPO10h','P10','POO9h','OI1h','OI2h','POO10h'], sep=",")
k.columns = ['AFp2','F9','AFF5h','AFF1h','AFF2h','AFF6h','F10','FFT9h','FFT7h','FFC5h','FFC3h','FFC1h','FFC2h','FFC4h','FFC6h','FFT8h','FFT10h','FC1','FCz','FC2','FTT9h','FTT7h','FCC5h','FCC3h','FCC1h','FCC2h','FCC4h','FCC6h','FTT8h','FTT10h','Cz','TTP7h','CCP5h','CCP3h','CCP1h','CCP2h','CCP4h','CCP6h','TTP8h','CPz','TPP9h','TPP7h','CPP5h','CPP3h','CPP1h','CPP2h','CPP4h','CPP6h','TPP8h','TPP10h','Pz','PPO1h','PPO2h','P9','PPO9h','POO1','POO2','PPO10h','P10','POO9h','OI1h','OI2h','POO10h']
我不知道这种做法是否有用,但我尝试让 k 包含数据并 k.columns 调用列。
然后我使用下面的for循环:
for columns in k:
freqs, psd = signal.welch(k[columns], fs=500,
window='hanning',nperseg=40, noverlap=20, scaling='density', average='mean')
plt.figure(figsize=(5, 4))
plt.plot(freqs, psd)
plt.title('PSD: power spectral density')
plt.xlabel('Frequency')
plt.ylabel('Power')
plt.axis([0,50, -1, 5])
plt.show()
如何在包含电极名称的绘图标题中添加循环?
非常感谢您的宝贵帮助! :)
@Mr.T 的回复真的很有帮助!!
使用 f 字符串格式 plt.title(f'PSD: power spectral density for {columns}')
?您可能还会从熟悉子图和轴对象中受益。 – T先生
我是 python 的新手,正在尝试通过 for 循环为我的 EEG 数据集的每个电极在单独的图中绘制 PSD。图的标题应包括相应的电极名称。
这是我用来从 .txt 文件加载数据的代码:
k = pd.read_csv(r'C:\Users\LPC\Desktop\rest txt 7min\AB24_rest_asr_ICA_MARA_7min.txt',usecols=['AFp2','F9','AFF5h','AFF1h','AFF2h','AFF6h','F10','FFT9h','FFT7h','FFC5h','FFC3h','FFC1h','FFC2h','FFC4h','FFC6h','FFT8h','FFT10h','FC1','FCz','FC2','FTT9h','FTT7h','FCC5h','FCC3h','FCC1h','FCC2h','FCC4h','FCC6h','FTT8h','FTT10h','Cz','TTP7h','CCP5h','CCP3h','CCP1h','CCP2h','CCP4h','CCP6h','TTP8h','CPz','TPP9h','TPP7h','CPP5h','CPP3h','CPP1h','CPP2h','CPP4h','CPP6h','TPP8h','TPP10h','Pz','PPO1h','PPO2h','P9','PPO9h','POO1','POO2','PPO10h','P10','POO9h','OI1h','OI2h','POO10h'], sep=",")
k.columns = ['AFp2','F9','AFF5h','AFF1h','AFF2h','AFF6h','F10','FFT9h','FFT7h','FFC5h','FFC3h','FFC1h','FFC2h','FFC4h','FFC6h','FFT8h','FFT10h','FC1','FCz','FC2','FTT9h','FTT7h','FCC5h','FCC3h','FCC1h','FCC2h','FCC4h','FCC6h','FTT8h','FTT10h','Cz','TTP7h','CCP5h','CCP3h','CCP1h','CCP2h','CCP4h','CCP6h','TTP8h','CPz','TPP9h','TPP7h','CPP5h','CPP3h','CPP1h','CPP2h','CPP4h','CPP6h','TPP8h','TPP10h','Pz','PPO1h','PPO2h','P9','PPO9h','POO1','POO2','PPO10h','P10','POO9h','OI1h','OI2h','POO10h']
我不知道这种做法是否有用,但我尝试让 k 包含数据并 k.columns 调用列。
然后我使用下面的for循环:
for columns in k:
freqs, psd = signal.welch(k[columns], fs=500,
window='hanning',nperseg=40, noverlap=20, scaling='density', average='mean')
plt.figure(figsize=(5, 4))
plt.plot(freqs, psd)
plt.title('PSD: power spectral density')
plt.xlabel('Frequency')
plt.ylabel('Power')
plt.axis([0,50, -1, 5])
plt.show()
如何在包含电极名称的绘图标题中添加循环?
非常感谢您的宝贵帮助! :)
@Mr.T 的回复真的很有帮助!!
使用 f 字符串格式 plt.title(f'PSD: power spectral density for {columns}')
?您可能还会从熟悉子图和轴对象中受益。 – T先生