Python 创建数据帧子集的散点图

Python 创建数据帧子集的散点图,python,pandas,dataframe,Python,Pandas,Dataframe,假设我有以下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'

假设我有以下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
我想做的是绘制X-Y数据的两个并排散点图,其中左侧散点图是“cond=A,Array=TT”组合的X-Y坐标数据,右侧散点图是“cond=B,Array=S”组合的X-Y坐标数据


这不是我实际使用的数据集,而是我正在使用的一个更大集合的占位符。我知道如何用python绘制散点图,但我不确定如何根据两个标签(cond和Array)从本质上提取我想要使用的坐标子集。我知道iloc有一种方法,但考虑到我需要处理的实际数据集是巨大的,而对于较大的数据集来说,找到这些数字将是一件非常痛苦的事情,我希望有一种更简单的方法。任何帮助都将不胜感激

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


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

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

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

给你:

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()
输出:


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

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()
输出:

给你:

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()
输出:


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

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()
输出:


我会将我想要的组合保存在
列表中
,并让熊猫的
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()

我会将我想要的组合保存在
列表中
,并让熊猫的
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()