【问题标题】:How to prevent seaborn FacetGrid from overlapping when wrapped如何防止seaborn FacetGrid在包裹时重叠
【发布时间】:2020-04-07 17:25:02
【问题描述】:

我有以下数据:

data = pd.read_json(
    '{"var":{"0":"first","1":"first","2":"first","3":"first","4":"first","5":"first","6":"first","7":"first","8":"first","9":"first","10":"second","11":"second","12":"second","13":"second","14":"second","15":"second","16":"second","17":"second","18":"second","19":"second","20":"third","21":"third","22":"third","23":"third","24":"third","25":"third","26":"third","27":"third","28":"third","29":"third","30":"fourth","31":"fourth","32":"fourth","33":"fourth","34":"fourth","35":"fourth","36":"fourth","37":"fourth","38":"fourth","39":"fourth"},"level":{"0":"0_thing that has a long name","1":"1_thing that has a long name","2":"2_thing that has a long name","3":"3_thing that has a long name","4":"4_thing that has a long name","5":"5_thing that has a long name","6":"6_thing that has a long name","7":"7_thing that has a long name","8":"8_thing that has a long name","9":"9_thing that has a long name","10":"0_thing that has a long name","11":"10_thing that has a long name","12":"6_thing that has a long name","13":"1_thing that has a long name","14":"3_thing that has a long name","15":"11_thing that has a long name","16":"4_thing that has a long name","17":"12_thing that has a long name","18":"5_thing that has a long name","19":"8_thing that has a long name","20":"13_thing that has a long name","21":"14_thing that has a long name","22":"15_thing that has a long name","23":"2_thing that has a long name","24":"16_thing that has a long name","25":"17_thing that has a long name","26":"18_thing that has a long name","27":"19_thing that has a long name","28":"20_thing that has a long name","29":"21_thing that has a long name","30":"13_thing that has a long name","31":"14_thing that has a long name","32":"15_thing that has a long name","33":"17_thing that has a long name","34":"2_thing that has a long name","35":"16_thing that has a long name","36":"20_thing that has a long name","37":"19_thing that has a long name","38":"18_thing that has a long name","39":"21_thing that has a long name"},"perc":{"0":24.3984831429,"1":13.5578842078,"2":10.3199382674,"3":14.1345077706,"4":23.3194885437,"5":14.96065323,"6":6.9737304361,"7":4.5652339327,"8":14.6795644283,"9":7.1649849158,"10":25.1593226794,"11":26.7421480151,"12":25.4425967424,"13":13.0877463565,"14":22.5365146082,"15":6.9683523649,"16":4.3323141006,"17":5.6351294027,"18":19.7448577749,"19":18.313549886,"20":26.0610663959,"21":19.9363641877,"22":15.1851539498,"23":11.9681521323,"24":19.6658384346,"25":16.0016010889,"26":10.1760876224,"27":15.7193904379,"28":14.3090339426,"29":7.1062130393,"30":19.1928731445,"31":14.8213673984,"32":18.6516148253,"33":18.8066683576,"34":8.068435507,"35":7.2176894958,"36":12.3757903962,"37":15.6977275207,"38":12.0725474142,"39":16.1457855035}}'
)

看起来像:

data.shape
(40,3)
print(data.sample(2, random_state=1))


       var                          level       perc
2    first   2_thing that has a long name  10.319938
31  fourth  14_thing that has a long name  14.821367

我正在使用以下内容创建一个情节:

sns.set(font_scale=1)
sns.set_style("white")

p = sns.catplot(
    data=data,
    x="level",
    y="perc",
    col="var",
    col_wrap=2,
    kind="bar",
    hue="var",
    sharex=False,
    dodge=False,
    sharey=False,
)

for axes in p.axes.flat:
    axes.set_xticklabels(
        axes.get_xticklabels(), rotation=65, horizontalalignment="right"
    )

plt.tight_layout()
p.set_xlabels("")

p.fig.set_figwidth(15)
p.fig.set_figheight(5)
type(p)

哪个输出:

我不确定如何在图之间留出空间,以便它们不会像上面那样重叠。

【问题讨论】:

    标签: python pandas matplotlib plot seaborn


    【解决方案1】:

    我会做一个sharex=True 以便隐藏上部子图的刻度标签:

    sns.set(font_scale=1)
    sns.set_style("white")
    
    p = sns.catplot(
        data=data,
        x="level",
        y="perc",
        col="var",
        col_wrap=2,
        kind="bar",
        hue="var",
        sharex=True,     # different to your code
        dodge=False,
        sharey=False,
    )
    
    for axes in p.axes.flat:
        axes.set_xticklabels(
            axes.get_xticklabels(), rotation=65, horizontalalignment="right"
        )
    
    # plt.tight_layout()
    p.set_xlabels("")
    
    p.fig.set_figwidth(15)
    p.fig.set_figheight(5)
    

    输出:

    【讨论】:

      【解决方案2】:

      你可以使用:

      p.fig.subplots_adjust(hspace=0.9, wspace=.15)
      

      sns.set(font_scale=1)
      sns.set_style("white")
      p = sns.catplot(
          data=data,
          x="level",
          y="perc",
          col="var",
          col_wrap=2,
          kind="bar",
          hue="var",
          sharex=False,
          dodge=False,
          sharey=False,
      )
      
      for axes in p.axes.flat:
          axes.set_xticklabels(
              axes.get_xticklabels(), rotation=65, horizontalalignment="right"
          )
      
      p.set_xlabels("")
      
      p.fig.set_figwidth(15)
      p.fig.set_figheight(10)
      # p.fig.tight_layout()
      p.fig.subplots_adjust(hspace=0.9, wspace=.15)
      
      plt.show()
      

      【讨论】:

        猜你喜欢
        • 2016-08-12
        • 1970-01-01
        • 1970-01-01
        • 2020-09-24
        • 1970-01-01
        • 2016-07-11
        • 2015-09-26
        • 2017-06-29
        • 2020-12-15
        相关资源
        最近更新 更多