0

I plot a seaborn countplot with gender that contains 2 categories (1: Male, 2: Female).

fig01 = sb.countplot(x = df.gender.values)

fig01.set_xticklabels(['Male', 'Female'])
fig01.set_yticklabels(['{}M'.format(int(num)) for num in fig01.get_yticks()/1e6]);

A warning appears right after the code.

<ipython-input-36-4627ceab9a45>:4: UserWarning: FixedFormatter should only be used together with FixedLocator
  fig01.set_yticklabels(['{}M'.format(int(num)) for num in fig01.get_yticks()/1e6]);

However, countplot looks fine.

enter image description here

I found relevant answer for similar question here. The guideline in the post did mention that I need to take care of the set_yticks() first then set_yticklabels(). I'm curious why no warning message for set_xticks() although I manually change x_ticklabels() to ['Male', 'Female]?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Ong K.S
  • 229
  • 1
  • 4
  • 15
  • The warning is there because setting tick labels without explicitly setting tick positions is very fragile. Changes to the plot may have undesired effects. Also, it complicates operations such as zooming. Feel free to ignore the warning. Probably there is no warning for the x-axis because seaborn explicitly sets the tick positions as one per bar. The recommended way to handle this, is to use a `FuncFormatter`. See e.g. [set y-axis in millions](https://stackoverflow.com/questions/61330427/set-y-axis-in-millions). – JohanC Jan 31 '21 at 15:38
  • Does this answer your question? [set y-axis in millions](https://stackoverflow.com/questions/61330427/set-y-axis-in-millions) – JohanC Jan 31 '21 at 15:38
  • @JohanC solution in the post only shows the way to format `xticklabels` with 'K'. It doesn't do the `set_xticks()` before `set_xticklabels()`. This is the part I'm curious. Perhaps you can guide me on setting `xticks()` from `get_xticks()`. `get_yticks()` in my chart is an `array([ 0., 2000000., 4000000., 6000000., 8000000., 10000000., 12000000., 14000000., 16000000.])`. – Ong K.S Feb 01 '21 at 09:11
  • Well, the recommended approach is *not* to use `set_xticks` nor `set_xticklabels`, but to use [tick locators](https://matplotlib.org/3.3.3/gallery/ticks_and_spines/tick-locators.html) with or without [tick formatters](https://matplotlib.org/3.3.3/gallery/ticks_and_spines/tick-formatters.html). The main problem that is solved by these formatters and locators is that they don't get messed up when other plot functions are called nor with zooming etc. On the other hand, the effects of `set_xticks` and `set_xticklabels` can get destroyed suddenly, They are part of an old interface. – JohanC Feb 01 '21 at 20:47

0 Answers0