代码之家  ›  专栏  ›  技术社区  ›  posdef

seaborn.destine()否定了将y轴移动到图形右侧的效果

  •  1
  • posdef  · 技术社区  · 6 年前

    有很多关于创建一个具有两个或更多y轴的绘图的例子,但是将一个单一的y轴移动到绘图的右侧看起来有点复杂。

    import numpy as np
    from matplotlib import pyplot as plt
    import seaborn as sns
    %matplotlib inline
    
    values = np.random.randint(1,20,100)
    
    f = plt.figure()
    ax = f.add_subplot(111)
    # sns.despine(offset=10) <- problem here...
    
    ax.yaxis.tick_right()
    ax.yaxis.set_ticks_position('both')
    sns.distplot(values)
    

    上面的代码片段创建了一个右边带有勾号的图,但是如果您想藐视该图(取消对所示行的注释),则如下所示:

    enter image description here

    旁白:在右边的轴后面的动机是因为我把这个和另一个并排的图绘制在一起,我希望y轴在子块的两边,而不是在它们之间。

    有什么主意吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Diziet Asahi    6 年前

    seaborn's despine has arguments 以便选择要隐藏的脊椎(默认情况下,除了左边的脊椎外,其他所有脊椎)。

    上、右、左、下:布尔型,可选

    如果是真的,移除脊椎。

    import numpy as np
    from matplotlib import pyplot as plt
    import seaborn as sns
    %matplotlib inline
    
    values = np.random.randint(1,20,100)
    
    f = plt.figure()
    ax = f.add_subplot(111)
    sns.despine(offset=10, left=True, right=False)  # <-- only show the right spine
    ax.yaxis.tick_right()
    ax.yaxis.set_ticks_position('right')  # <--- I also edited this line to have tick marks only on the right
    sns.distplot(values)