如何覆盖两个海生地块?

2024-04-24 06:35:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数据集(考虑下面的MWE),我想从中创建具有不同样式参数的RelPlot

from io import StringIO
import numpy as np
import pandas as pd
import seaborn as sns

data1 = """index   x   y   group    ground_x_A    ground_y_A     ground_x_B    ground_y_B
0    27    26  A   20  15   40  45
1    26    27  A   20  15   40  45
2    28    22  A   20  15   40  45
3    25    24  A   20  15   40  45
4    29    31  A   20  15   40  45
5    46    29  B   20  15   40  45
6    39    37  B   20  15   40  45
7    38    41  B   20  15   40  45
8    44    46  B   20  15   40  45
9    46    38  B   20  15   40  45
10   37    33  B   20  15   40  45"""

df = pd.read_csv(StringIO(data1), index_col=[0], sep=" ", skipinitialspace=True)

sns.set_style("whitegrid")

sns.relplot(data = df, x = 'x', y = 'y', hue = 'group', s = 25)
sns.relplot(data = df, x = 'ground_x_A', y = 'ground_y_B', s = 100)
sns.relplot(data = df, x = 'ground_x_B', y = 'ground_y_B', s = 100)

我想将生成的图(即,将它们组合成一个图)与图2和图2中的点进行叠加;3匹配图1中每组的颜色。有人能给我指出正确的方向吗

(我尝试将here引入的解决方案调整到我的示例中,但未成功。)


Tags: 数据importdfdataindexasgroup样式
2条回答

考虑将数据重组为长格式,{{CD1>}为单个{{CD2>},其中包含两行,用于新的^ ^ {CD3>}值,用于^ {< CD4>}分解。

main_df = df.loc[:, ['x', 'y', 'group']]

ground_A_df = (df.loc[:, ['ground_x_A', 'ground_y_A']]
                 .assign(group = 'ground_A')
                 .set_axis(['x', 'y', 'group'], axis='columns')
                 .drop_duplicates())
                
ground_B_df = (df.loc[:, ['ground_x_B', 'ground_y_B']]
                 .assign(group = 'ground_B')
                 .set_axis(['x', 'y', 'group'], axis='columns')
                 .drop_duplicates())
                
# BUILD LONG FORM DATASET           
long_df = pd.concat([main_df, ground_A_df, ground_B_df], ignore_index=True)


sns.set_style("whitegrid")
sns.relplot(data = long_df, x = 'x', y = 'y', hue = 'group', s = 25)

Plot Output


可选地,考虑将^ {< CD5>}参数和区分^ {CD6>}点作为比其他数据点大的圆:

main_df = (df.loc[:, ['x', 'y', 'group']]
             .assign(ground = lambda x: x['group'], size=25))

# DRY-er CODE WITH LIST COMPREHENSION
ground_dfs = [(df.loc[:, [f'ground_x_{i}', f'ground_y_{i}']]
                 .assign(group = i, ground = i, size=100)
                 .set_axis(['x', 'y', 'group', 'ground', 'size'], axis='columns')
                 .drop_duplicates())
               for i in list('AB')] 
                
# BUILD LONG FORM DATASET WITH NEW COLUMNS FOR ground AND size
long_df = pd.concat([main_df] + ground_dfs, ignore_index=True)

sns.set_style("whitegrid")
sns.relplot(data = long_df, x = 'x', y = 'y', col = 'ground', 
            hue = 'group', size = 'size', legend = 'full')

Plot Output

relplot在每次调用时创建一个新图形(aFacetGrid),因此不能使用该图形。也就是说,relplot()使用scatterplot()进行实际绘图

palette={'A':'C3', 'B':'C4'}

sns.scatterplot(data = df, x = 'x', y = 'y', hue = 'group', s = 25, palette=palette)
sns.scatterplot(data = df, x = 'ground_x_A', y = 'ground_y_B', s = 100, color=palette['A'])
sns.scatterplot(data = df, x = 'ground_x_B', y = 'ground_y_B', s = 100, color=palette['B'])

enter image description here

相关问题 更多 >