【问题标题】:seaborn pointplot and boxplot in one plot but shifted on the x-axisseaborn 点图和箱线图在一个图中,但在 x 轴上移动
【发布时间】:2022-05-06 23:37:12
【问题描述】:

我想在一个图中同时绘制箱线图和平均值。到目前为止,我的情节看起来像这样使用这些代码行:

sns.swarmplot(x="stimulus", y="data", data=spi_num.astype(np.float), edgecolor="black", linewidth=.9)
sns.boxplot(x="stimulus", y="data", data=spi_num.astype(np.float), saturation=1)
sns.pointplot(x="stimulus", y="data", data=spi_num.astype(np.float), linestyles='', scale=1, color='k', errwidth=1.5, capsize=0.2, markers='x')
sns.pointplot(x="stimulus", y="data", data=spi_num.astype(np.float), linestyles='--', scale=0.4, color='k', errwidth=0, capsize=0)
plt.ylabel("number of spikes")
plt.title("Median Number of Spikes");

我想将我的平均“x”标记向右移动一点,这样误差线就不会与箱线图中的胡须重叠。知道怎么做吗?一个额外的问题:我如何在这个情节中插入一个图例,优雅地说“x:平均值,o:数据值”?


构建我的数据框

trial_vec    = np.tile(np.arange(16)+1, 10)     
stimulus_vec = np.repeat([-2., -1.75, -1., -0.75, -0.5,  0.5,  1.,  1.25,  1.75,  2.5 ], 16)                  
data_vec     = np.random.randint(0, 16, size=160)
spi_num      = pd.DataFrame({'trial': trial_vec, 'stimulus': stimulus_vec, 'data': data_vec}).astype('object')

【问题讨论】:

    标签: python matplotlib legend seaborn boxplot


    【解决方案1】:

    为了移动绘图上的点,可以使用变换;在这种情况下,ScaledTranslation 很有用。不幸的是,seaborn 不允许直接使用变换,也不允许访问绘制的对象。因此,需要从轴中获取绘制的对象(在本例中为 PathCollection)。如果要偏移的图是坐标轴ax 中的第一个图,我们可以简单地通过ax.collections[0] 得到它。然后我们可以通过.set_transform 对其进行转换。

    fig, ax = plt.subplots()
    sns.pointplot(... , ax=ax)
    #produce transform with 5 points offset in x direction
    offset = transforms.ScaledTranslation(5/72., 0, ax.figure.dpi_scale_trans)
    trans = ax.collections[0].get_transform()
    ax.collections[0].set_transform(trans + offset)
    

    完整代码:

    import pandas as pd
    import numpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt
    import matplotlib.transforms as transforms
    
    
    trial_vec    = np.tile(np.arange(16)+1, 10)     
    stimulus_vec = np.repeat([-2., -1.75, -1., -0.75, -0.5,  0.5,  1.,  1.25,  1.75,  2.5 ], 16)
    data_vec     = np.random.randint(0, 16, size=160)
    spi_num      = pd.DataFrame({'trial': trial_vec, 
                                 'stimulus': stimulus_vec, 'data': data_vec})
    
    fig, ax = plt.subplots()
    
    sns.pointplot(x="stimulus", y="data", data=spi_num, linestyles='', scale=1, 
                  color='k', errwidth=1.5, capsize=0.2, markers='x', ax=ax)
    #produce transform with 5 points offset in x direction
    offset = transforms.ScaledTranslation(5/72., 0, ax.figure.dpi_scale_trans)
    trans = ax.collections[0].get_transform()
    ax.collections[0].set_transform(trans + offset)
    
    sns.swarmplot(x="stimulus", y="data", data=spi_num, edgecolor="black", linewidth=.9, ax=ax)
    sns.boxplot(x="stimulus", y="data", data=spi_num, saturation=1, ax=ax)
    sns.pointplot(x="stimulus", y="data", data=spi_num, linestyles='--', scale=0.4, 
                  color='k', errwidth=0, capsize=0, ax=ax)
    plt.ylabel("number of spikes")
    plt.title("Median Number of Spikes");
    
    plt.show()
    

    要同时移动线图,您需要对其散点 (ax.collections[1]) 和图中的所有线 (ax.lines) 执行与上述相同的操作

    sns.pointplot(x="stimulus", y="data", data=spi_num, linestyles='--', scale=0.4, 
                  color='k', errwidth=0, capsize=0, ax=ax, gid="Nm")
    # shift points of connecting line:
    trans = ax.collections[1].get_transform()
    ax.collections[1].set_transform(trans + offset)
    # shift everything else:
    for line in ax.lines:
        trans = line.get_transform()
        line.set_transform(trans + offset)
    

    【讨论】:

    • 如何用我的“x”移动虚线?还有错误栏?到目前为止,只有我的标记被移动了,但这看起来很奇怪。
    • 我的意思是用你的代码我移动了'x'标记。但我不能移动连接它们的虚线,我也不能移动误差线。
    • 我的意思是,你尝试过什么?我已经向您展示了移动标记的概念。你必须对这些行做同样的事情。如果您尝试这样做并遇到问题,我可以帮助您解决它。
    • 所以我使用了你的代码。在第一个点图中,我刚刚插入了 linestyles='--'。我删除了第二个点图。因此,产生“x”标记的同一命令会产生虚线。然而,代码仅移动标记。为什么?
    • 标记是collection 的一部分,因此您需要移动collection。错误栏和线是lines,因此您需要移动通过ax.lines 获得的lines
    猜你喜欢
    • 2019-12-07
    • 2023-04-10
    • 1970-01-01
    • 2021-06-24
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 2020-05-24
    相关资源
    最近更新 更多