创建数据帧子集的散点图

时间:2019-11-08 20:39:04

标签: python pandas dataframe

假设我以以下数据框为例:

import numpy as np
import pandas as pd
df = pd.DataFrame({
   'cond': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B','B', 'B', 'B', 'B', 'B','B','B'],
   'Array':  ['S', 'S', 'TT', 'TT','S', 'S', 'TT', 'TT','S', 'S', 'TT', 'TT','S', 'S', 'TT', 'TT','SS','TT'],
   'X':  [1, 2, 3, 1, 2 , 3, 4, 7.3, 5.1, 3.2, 1.4, 5.5, 9.9, 3.2, 1.1, 3.3, 1.2, 5.4],
   'Y':  [3.1, 2.2, 2.1, 1.2,  2.4, 1.2, 1.5, 1.33, 1.5, 1.6, 1.4, 1.3, 0.9, 0.78, 1.2, 4.0, 5.0, 6.0],
   'Marker':  [2.0, 1.2, 1.2, 2.01, 2.55, 2.05, 1.66, 3.2, 3.21, 3.04, 8.01, 9.1, 7.06, 8.1, 7.9, 5.12, 5.23, 5.15],
   'Area': [3.0, 2.0, 2.88, 1.33,  2.44, 1.25, 1.53, 1.0, 0.156, 2.0, 2.4, 6.3, 6.9, 9.78, 10.2, 15.0, 16.0, 19.0]
})
print(df)

这将产生一个如下所示的集合:

   cond Array    X     Y  Marker    Area
0     A     S  1.0  3.10    2.00   3.000
1     A     S  2.0  2.20    1.20   2.000
2     A    TT  3.0  2.10    1.20   2.880
3     A    TT  1.0  1.20    2.01   1.330
4     A     S  2.0  2.40    2.55   2.440
5     A     S  3.0  1.20    2.05   1.250
6     A    TT  4.0  1.50    1.66   1.530
7     A    TT  7.3  1.33    3.20   1.000
8     A     S  5.1  1.50    3.21   0.156
9     B     S  3.2  1.60    3.04   2.000
10    B    TT  1.4  1.40    8.01   2.400
11    B    TT  5.5  1.30    9.10   6.300
12    B     S  9.9  0.90    7.06   6.900
13    B     S  3.2  0.78    8.10   9.780
14    B    TT  1.1  1.20    7.90  10.200
15    B    TT  3.3  4.00    5.12  15.000
16    B    SS  1.2  5.00    5.23  16.000
17    B    TT  5.4  6.00    5.15  19.000

我想做的是绘制XY数据的两个并排散点图,其中左侧散点图是“ cond = A,Array = TT”组合的XY坐标数据,右侧散点图是“ cond = B,Array = S”组合。

这不是我的实际数据集,而是我正在使用的更大集合的占位符。我知道如何在python中进行散点图绘制,但是我不确定该如何基于两个标签(cond和Array)提取要使用的坐标子集。我知道iloc有一种方法,但是鉴于我需要使用的实际数据集非常庞大,而对于较大的数据集来说,找到这些数字将是一个巨大的痛苦,我希望有一种更简单的方法。任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:2)

您在这里:

fig,axes = plt.subplots(1,2)

df.loc[df['cond'].eq('A') & df['Array'].eq('TT')].plot.scatter(x='X',y='Y', ax=axes[0])
df.loc[df['cond'].eq('B') & df['Array'].eq('S')].plot.scatter(x='X',y='Y', ax=axes[1])

plt.show()

输出:

enter image description here


更新:我注意到您的数据中有一列Marker。因此,如果您想根据Marker的大小来缩放散点图:

fig,axes = plt.subplots(1,2)

ss = [df['cond'].eq('A') & df['Array'].eq('TT'),
     df['cond'].eq('B') & df['Array'].eq('S')]

for ax, s in zip(axes, ss):
    df.loc[s].plot.scatter(x='X',y='Y', s=df.loc[s,'Marker']*10, ax=ax)

plt.show()

输出:

enter image description here

答案 1 :(得分:1)

您还可以尝试使用df.iterrows()遍历所有行:

x1, y1 = [], []
x2, y2 = [], []

for i,row in df.iterrows():
    if row["cond"] == "A" and row["Array"] == "TT":
        x1.append(row["X"])
        y1.append(row["Y"])
    elif row["cond"] == "B" and row["Array"] == "S":
        x2.append(row["X"])
        y2.append(row["Y"])

然后使用x1,y1绘制您的第一个组合,并使用x2,y2绘制您的第二个组合。

答案 2 :(得分:1)

我将想要的组合保留在list中,然后让Pandas的groupby跟踪组的索引。然后,我可以遍历我喜欢的组合并查找关联的索引是什么。创建的groupby对象包含一个字典,其中的值是我切片原始数据帧所需的确切索引。

import numpy as np
import matplotlib.pyplot as plt

to_plot = [('A', 'TT'), ('B', 'S')]
fig, axes = plt.subplots(1, len(to_plot), figsize=(10, 5), sharey=True)

g = df.groupby(['cond', 'Array'])
for i, (c, a) in enumerate(to_plot):
    df.loc[g.groups[(c, a)]].plot.scatter(
        'X', 'Y', title=f'cond: {c} -- Array {a}', ax=axes[i]
    )

fig.tight_layout()

enter image description here