【问题标题】:Plot iteratively single variable from multiple dataframe using matplotlib使用 matplotlib 从多个数据框中迭代地绘制单个变量
【发布时间】: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


    【解决方案1】:

    我猜该文件无法正确读取。尝试使用

    pd.read_table(f, delim_whitespace=True)
    

    您在创建图之前创建子图。这需要扭转。

    接下来,如果type(row['age']) 不是int 怎么办?

    如果您可以确保row['age'] 包含ints,那么下一个问题将是您尝试将单个点绘制为线图。

    使用

    plt.plot(age,10, marker="o")
    

    使得该点附有一个可以显示的标记。

    总的来说,代码似乎可以变得更加紧凑;所以以下内容应该可以满足您的需求。

    u = u"""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"""
    
    import io
    import pandas as pd
    import glob
    import matplotlib.pyplot as plt
    
    
    filelist = [io.StringIO(u)]
    
    fig, ax = plt.subplots()
    
    for f in filelist:
        df = pd.read_table(f, delim_whitespace=True)
        if 'age' in df.columns:
            #df = df[df["age"] != "n/a"] # remove n/a values or
            df = df[~df["age"].isin(["n/a"])]
            plt.plot(df['age'], [3]*len(df), marker="o", ls="")
    
    plt.show()
    

    【讨论】:

    • 我添加了一个示例 tsv 文件
    • 如何避免将n/a 转换为数值
    • 不,上面的代码将所有文件中的数据绘制到同一个图中。
    • 也许是一种不同的方法df = df[~df["age"].isin(["n/a"])]
    • 我认为您误解了 SO 的工作原理。这不是聊天室或论坛。您提出一个包含所有信息的问题,然后有人给出答案。因此,如果您有问题,请提出问题。但请确保之前已搜索过解决方案并在问题中包含所有必要的详细信息。
    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多