-1

I am trying to have seaborn facetgrid subplots with rotated axis, but I can't seem to rotate all the axis, only the last one. Any tips? The code below leaves the chart with just the last subplot with a rotate axis.

# Create a grid : initialize it
g = sns.FacetGrid(df_full_year, col='segment', hue='segment', col_wrap=4,sharey=False,sharex=False)

# Add the line over the area with the plot function
g = g.map(plt.bar, 'Currency', 'received_usd')
 
# Fill the area with fill_between
g = g.map(plt.bar, 'Currency', 'received_usd', alpha=.6).set_titles("{col_name} country")
 
# Control the title of each facet
g = g.set_titles("{col_name}")
 
# Add a title for the whole plot
plt.subplots_adjust(top=.92)
g = g.fig.suptitle('Evolution of the value of stuff in 16 countries')

plt.xticks(rotation=45)

# Show the graph
plt.show()
Trenton McKinney
  • 43,885
  • 25
  • 111
  • 113
Kim Grauer
  • 13
  • 1
  • 1
    Does this answer your question? [Rotate x-axis labels FacetGrid seaborn not working](https://stackoverflow.com/questions/60077401/rotate-x-axis-labels-facetgrid-seaborn-not-working) – JohanC Sep 20 '21 at 12:49
  • 1
    Please don't constantly assign `g` to the result of other functions. Just start with `g = sns.FacetGrid(...)` and the leave it alone. You can use `g.set_xticklabels(rotation=45)`, see e.g. [How to rotate xticklabels in a seaborn catplot](https://stackoverflow.com/questions/63909351/how-to-rotate-xticklabels-in-a-seaborn-catplot) – JohanC Sep 20 '21 at 12:55
  • Thank you, but it does not. The code above only rotates the axis on the very last chart in the facetgrid. I can use catplot, but was just wondering why it only rotates the data on the very last chart. – Kim Grauer Sep 20 '21 at 18:52
  • Supposing you are using the latest seaborn and matplotlib versions, `g.set_xticklabels(rotation=45)` does rotate all visible tick labels of all subplots. But you do need to remove `g = ` from `g = g.fig.suptitle(...)` because the result of `g.fig.suptitle()` isn't a `FacetGrid` anymore. The linked post about `catplot` doesn't mean you need to use `catplot`. `catplot` is just another function that returns a `FacetGrid`, for which the label rotations can be set in the same way. – JohanC Sep 20 '21 at 20:12

1 Answers1

0

The x tick labels can be rotated with g.set_xticklabels(rotation=45). An error in the code is the assignment g = g.fig.suptitle(...), because g.fig.suptitle(...) returns the text element of the suptitle, and not a FacetGrid. All other assignments to g, except the first, should be removed as they aren't useful and introduce potential errors. Note that with g.fig.tight_layout() the white space will be adapted, so everything fits nicely.

Here is some example code, starting from dummy test data:

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

df_full_year = pd.DataFrame({'segment': np.repeat(np.arange(1, 9), 10),
                             'Currency': np.tile([*'abcdefghij'], 8),
                             'received_usd': np.random.randint(20, 50, 80)})

g = sns.FacetGrid(df_full_year, col='segment', hue='segment', col_wrap=4, sharey=False, sharex=False)

g.map(plt.bar, 'Currency', 'received_usd')
g.set_titles("{col_name} country")
g.fig.suptitle('Evolution of the value of stuff in 16 countries')

g.set_xticklabels(rotation=45)

g.fig.tight_layout()
plt.show()

FacetGrid with rotated x tick labels

JohanC
  • 59,187
  • 8
  • 19
  • 45