简体   繁体   中英

Change y-axis scale - FacetGrid

I cannot work out how to change the scale of the y-axis. 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

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.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

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

However this changed only the yticks of the upper row in my mockup example.
For this reason you probably want to change the y ticks based on the axis with ax.set_yticks() . To get the axis from your grid object you can implemented a list comprehension as follows:

 [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]

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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