10

In a statement like:

g = sns.FacetGrid(df, row="variable", col="state")

I would like to control the order in which the variables and the states appear in the FacetGrid. I can't find where this is even possible.

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
pitosalas
  • 10,286
  • 12
  • 72
  • 120
  • Note that we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened. – halfer May 02 '20 at 10:06
  • If you wouldn't mind sticking to the [preferred style here](https://meta.stackoverflow.com/questions/288160/no-thanks-damn-it), it would be appreciated. Have mercy on volunteer editors - there are not many of us, and there is much to edit. It is not an onerous request. – halfer May 02 '20 at 10:25

1 Answers1

12

You can use row_order and col_order:

from sklearn.datasets import load_iris
import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

data = load_iris()
df = pd.DataFrame(data.data, columns=['sepal.length','sepal.width','petal.length','petal.width'])
df['species'] = data.target
df['subset'] = np.random.choice(['A','B','C'],150,replace=True)

g = sns.FacetGrid(df, col="species", col_order=[0,2,1],row="subset",row_order=['C','B','A'])
g = g.map(plt.scatter, "sepal.length", "sepal.width")

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • thanks! @StupidWolf ... Do you have an idea about this related one: https://stackoverflow.com/questions/61541562/customizing-titles-in-seaborn-facetgrid-based-on-facet – pitosalas May 02 '20 at 21:37