代码之家  ›  专栏  ›  技术社区  ›  Elias Strehle

Seaborn FacetGrid:使轴相交于(0,0)

  •  0
  • Elias Strehle  · 技术社区  · 8 年前

    我在用西伯恩的 FacetGrid 散布绘制数据帧。 下面是一个简单的例子:

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    df = pd.DataFrame(np.random.randint(-100, 100, size=(100, 2)), columns=['x', 'y'])
    
    fig = sns.FacetGrid(data=df)
    fig.map(plt.scatter, 'x', 'y')
    

    enter image description here

    这使得轴在绘图左下角相交。我希望它们在(0,0)处相交。在matplotlib中,我会用 set_position() 脊柱的功能。但我不知道如何通过Seaborn访问这个功能。 如何更改轴在绘图中的相交位置?

    1 回复  |  直到 8 年前
        1
  •  3
  •   ImportanceOfBeingErnest    8 年前

    潜在的问题似乎是:如何获得matplotlib Axes 来自Seaborn's FacetGrid 反对?

    如果 g = seaborn.FacetGrid(...) ,然后 g.axes 是一个numpy数组 是的。这里有一个子块,因此数组的唯一项是要查找的轴,

    ax = g.axes[0,0]
    

    从这里你可以使用已知的解决方案通过 set_position ,如中所示 the spine placement demo 是的。

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    df = pd.DataFrame(np.random.randint(-100, 100, size=(100, 2)), columns=['x', 'y'])
    
    g = sns.FacetGrid(data=df)
    g.map(plt.scatter, 'x', 'y')
    
    ax = g.axes[0,0]
    ax.spines['left'].set_position('zero')
    ax.spines['bottom'].set_position('zero')
    
    plt.show()
    

    enter image description here