gpt4 book ai didi

python - 包裹时如何防止seaborn FacetGrid重叠

转载 作者:行者123 更新时间:2023-12-04 10:15:23 24 4
gpt4 key购买 nike

我有以下数据:

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)

输出:

enter image description here

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

最佳答案

我会做一个 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)

输出:

enter image description here

关于python - 包裹时如何防止seaborn FacetGrid重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61085809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com