通过跳过第一列并绘制多个图

时间:2017-09-25 11:13:01

标签: pandas iteration seaborn

我有一个数据框如下, df.head()

     ID AS_FP   AC_FP   RP11_FP RP11_be AC_be   AS_be   Info
AE02    0.060233    0   0.682884    0.817115    0.591182    0.129252    SAP
AE03    0   0   0   0.889181    0.670113    0.766243    SAP
AE04    0   0   0.033256    0.726193    0.171861    0.103839    others
AE05    0   0   0.034988    0.451329    0.431836    0.219843    others

我的目标是将每个列从AS_FP开始直到RP11_beta作为lmplot,每个x axis是以FP结尾的列,y axis是以{{1}结尾的对应列}}。 我想将它保存为单独的文件,所以我通过跳过第一列be来反复遍历列,就像这样,

ID

但我迷路了如何继续,我需要绘制

for ind, column in enumerate(df.columns):
    if column.split('_')[0] == column.split('_')[0]:

并将每个图像保存为单独的文件

1 个答案:

答案 0 :(得分:1)

直截了当的解决方案:

1)玩具数据:

import pandas as pd
from collections import OrderedDict
import matplotlib.pyplot as plt
import seaborn as sns

dct = OrderedDict()
dct["ID"] = ["AE02", "AE03", "AE04", "AE05"]
dct["AS_FP"] = [0.060233, 0, 0, 0]
dct["AC_FP"] = [0, 0,0, 0]
dct["RP11_FP"] = [0.682884, 0, 0.033256, 0.034988]
dct["AS_be"] = [0.129252, 0.766243, 0.103839, 0.219843]
dct["AC_be"] = [0.591182, 0.670113, 0.171861, 0.431836]
dct["RP11_be"] = [0.817115, 0.889181, 0.726193, 0.451329]
dct["Info"] = ["SAP", "SAP", "others", "others"]

df = pd.DataFrame(dct)

2)迭代对,用唯一的文件名保存每个数字:

graph_cols = [col for col in df.columns if ("_FP" in col) or ("_be" in col)]

fps = sorted([col for col in graph_cols if "_FP" in col])
bes = sorted([col for col in graph_cols if "_be" in col])

for x, y in zip(fps, bes):
    snsplot = sns.lmplot(x, y, data=df, fit_reg=False, hue='Info',
           size=10, scatter_kws={"s": 700})
    snsplot.savefig(x.split("_")[0] + ".png")

您可以根据需要在lmlplot中添加所需的参数。