简体   繁体   English

更改 y 轴刻度 - FacetGrid

[英]Change y-axis scale - FacetGrid

I cannot work out how to change the scale of the y-axis.我无法弄清楚如何更改 y 轴的比例。 My code is:我的代码是:

grid = sns.catplot(x='Nationality', y='count', 
                   row='Age', col='Gender', 
                   hue='Type',
                   data=dfNorthumbria2, kind='bar', ci='No')

目前看起来像这样

I wanted to just go up in full numbers rather than in.5我只想增加 go 的全数而不是 in.5

Update更新

I just now found this tutorial the probably easiest solution will be the following:我刚刚发现本教程可能最简单的解决方案如下:

grid.set(yticks=list(range(5)))

From the help of grid.setgrid.set的帮助下

Help on method set in module seaborn.axisgrid:
set(**kwargs) method of seaborn.axisgrid.FacetGrid instance
    Set attributes on each subplot Axes.


Since seaborn is build on top of matplotlib you can use yticks from plt 由于 seaborn 建立在 matplotlib 之上,因此您可以使用 plt 中的yticks

 import matplotlib.pyplot as plt plt.yticks(range(5))

However this changed only the yticks of the upper row in my mockup example.然而,这仅改变了我的模型示例中上排的 yticks。
For this reason you probably want to change the y ticks based on the axis with ax.set_yticks() .出于这个原因,您可能希望使用ax.set_yticks()更改基于轴的 y 刻度。 To get the axis from your grid object you can implemented a list comprehension as follows:要从grid object 中获取轴,您可以实现列表理解,如下所示:

 [ax[0].set_yticks(range(0,150,5) )for ax in grid.axes]

A full replicable example would look like this (adapted from here )一个完整的可复制示例如下所示(改编自此处

 import seaborn as sns import matplotlib.pyplot as plt sns.set(style="ticks") exercise = sns.load_dataset("exercise") grid = sns.catplot(x="time", y="pulse", hue="kind", row="diet", data=exercise) # plt.yticks(range(0,150,5)) # Changed only one y-axis # Changed y-ticks to steps of 20 [ax[0].set_yticks(range(0,150,20) )for ax in grid.axes]

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM