【发布时间】:2017-11-20 17:03:29
【问题描述】:
我正在尝试从多个数据框中绘制所有参与者的年龄。我想将所有数据框中的年龄绘制成一个图。所以最终的情节应该包含绘制每个年龄的数据点。
以下是我正在尝试的一段代码,但它给出的是空白图。
import pandas as pd
import glob
import matplotlib.pyplot as plt
%matplotlib inline
filelist = glob.glob('/Users/kadb/Desktop/participants_tsv_files/*.tsv')
# fig = plt.figure()
ax = fig.add_subplot(111)
# ax.xaxis.set_ticks(df.index)
# ax.xaxis.set_ticklabels(df['g'])
plt.figure()
for file in filelist:
df = pd.read_table(file)
if 'age' in df.columns:
df['age'] = pd.to_numeric(df['age'])
# df['age'] = df['age'].astype(str).convert_objects(convert_numeric=True)
# plt.plot(df['age'], 3)
for index, row in df.iterrows():
if type(row['age']) is int:
if row['age'] >= 0:
age = row['age']
plt.plot(age,10)
示例 tsv 文件:
participant_id gender age physioSampling restAcquisiotion
sub-01 M 26 50 after_cuedSGT
sub-02 M 21 50 after_cuedSGT
sub-03 M 22 50 after_cuedSGT
sub-04 M 23 50 after_cuedSGT
sub-05 M 21 50 before_cuedSGT
sub-06 M 19 50 before_cuedSGT
sub-07 F 18 50 before_cuedSGT
sub-08 F 21 50 before_cuedSGT
sub-09 M 20 40-60 before_cuedSGT
sub-10 F 21 50 before_cuedSGT
sub-11 F 20 50 before_cuedSGT
sub-12 M 21 50 before_cuedSGT
sub-13 F 31 50-60 before_cuedSGT
【问题讨论】:
标签: python pandas matplotlib plot