3

I created a plot using:

g = sns.catplot(x='loja', y='preco', col='Descr. Grupo', col_wrap=3, capsize=.2, height=4, aspect=1.2,
            kind='point', sharey=False, data=df_long)

This plot builds 10 grids, but only the last 3 ones has the xticklabels that I want.
We can see this as follow:

for ax in g.axes:
    print(ax.get_xticklabels())   
<a list of 0 Text major ticklabel objects>
<a list of 0 Text major ticklabel objects>
<a list of 0 Text major ticklabel objects>
<a list of 0 Text major ticklabel objects>
<a list of 0 Text major ticklabel objects>
<a list of 0 Text major ticklabel objects>
<a list of 0 Text major ticklabel objects>
<a list of 3 Text major ticklabel objects>
<a list of 3 Text major ticklabel objects>
<a list of 3 Text major ticklabel objects>

As the column loja has 3 different categories, I expect 3 xticklabels for every grid.

So I tried to do this:

for ax in g.axes:
    ax.set_xticklabels(['a', 'b', 'c'], visible=True, rotation=0)

And now the xticklabels are created, but there's an unexpected behaviour: the labels also appears in the top of every grid.

How can I keep only the xticklabels from the bottom of every grid?

igorkf
  • 3,159
  • 2
  • 22
  • 31

1 Answers1

6

Setting ax.tick_params(labelbottom=True) seems to solve the problem. Possibly you need plt.subplots_adjust(...) to widen some of the paddings.

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df_long = pd.DataFrame({'loja': np.random.randint(1, 4, 200),
                        'preco': np.random.randn(200).cumsum() + 100,
                        'Descr. Grupo': np.random.randint(1, 6, 200)})
g = sns.catplot(x='loja', y='preco', col='Descr. Grupo', col_wrap=3, capsize=.2, height=3, aspect=1.2,
                kind='point', sharey=False, data=df_long)
for ax in g.axes:
    ax.set_xticklabels(['A', 'B', 'C'], rotation=0)
    ax.tick_params(labelbottom=True)
plt.subplots_adjust(bottom=0.1, left=0.06, hspace=0.2)
plt.show()

example plot

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Yes, this is crazy...only setting `for ax in g.axes: ax.tick_params(labelbottom=True)` worked here. I don't needed to use `ax.set_xticklabels()` at all. Thank you. – igorkf Mar 10 '21 at 20:57