You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python散点图设计:基于另一列值选择X轴变量特定值

实现你的MissingLimb散点图指南

嗨,作为Python新手就开始尝试数据可视化真的很棒!我来帮你一步步用pandas和seaborn实现你想要的散点图~

第一步:整理你的数据集

首先咱们需要把数据从长格式(每个参与者两行数据)转换成宽格式(每个参与者一行,同时包含MissingLimb=0和1的mean值)。假设你的数据集有三列:Participant(参与者标识)、MissingLimb(0或1)、mean(对应均值),可以这样处理:

import pandas as pd

# 读取你的数据集(根据实际格式调整,比如csv/excel)
df = pd.read_csv('ErrorMedianScatter.csv')

# 转换为宽格式:每个参与者一行,列分别对应MissingLimb=0和1的mean
pivoted_df = df.pivot_table(
    index='Participant',  # 按参与者分组
    columns='MissingLimb',  # 把MissingLimb的取值拆成列
    values='mean'  # 要提取的数值列
).reset_index()

# 给列重命名,方便后续调用
pivoted_df.columns = ['Participant', 'Mean_MissingLimb_0', 'Mean_MissingLimb_1']

如果你的数据集列名和我假设的不一样,记得替换成实际的列名哦!如果有缺失值,可以用pivoted_df = pivoted_df.dropna()去除不完整的行。

第二步:用Seaborn绘制散点图

接下来咱们用seaborn画出X轴为MissingLimb=0的均值、Y轴为MissingLimb=1的均值的散点图:

import seaborn as sns
import matplotlib.pyplot as plt

# 设置seaborn的绘图风格(可选,让图更好看)
sns.set_style("whitegrid")

# 创建画布并设置大小
plt.figure(figsize=(8, 6))

# 绘制散点图
sns.scatterplot(
    data=pivoted_df,
    x='Mean_MissingLimb_0',  # X轴:MissingLimb=0的均值
    y='Mean_MissingLimb_1',  # Y轴:MissingLimb=1的均值
    alpha=0.7,  # 点的透明度,避免重叠时看不清
    s=80  # 点的大小,可选调整
)

# 添加坐标轴标签和标题
plt.xlabel('Mean Value (MissingLimb = 0)')
plt.ylabel('Mean Value (MissingLimb = 1)')
plt.title('Scatter Plot of Mean Values by MissingLimb Status')

# 可选:添加一条对角线,方便对比两个均值的大小
plt.plot(
    [pivoted_df['Mean_MissingLimb_0'].min(), pivoted_df['Mean_MissingLimb_0'].max()],
    [pivoted_df['Mean_MissingLimb_1'].min(), pivoted_df['Mean_MissingLimb_1'].max()],
    'r--',  # 红色虚线
    label='Equal Mean'
)
plt.legend()

# 显示图形
plt.show()

这样你就能得到想要的散点图啦!如果运行过程中有报错,大概率是列名不匹配或者数据格式问题,检查一下你的数据集列名是否和代码里的一致就好~

内容的提问来源于stack exchange,提问作者MDhar94

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:41:29