user308827
user308827

Reputation: 21961

Change legend order and color in seaborn facetgrid

I am using seaborn facetgrid in combination with lineplot to produce:

Is there a way to change legend order from Average, Good, Poor to Poor, Average, Good and assign the color red to Poor and the color green to Good?

Upvotes: 2

Views: 3904

Answers (2)

k.mcgee
k.mcgee

Reputation: 96

Building off of Quang Hoang's Answer, you can also add the keyword pallete to control the colors:

orders = ['Poor', 'Average', 'Good']
palette = {'Poor': 'red', 'Average': 'blue', 'Good': 'green'}

arg = ['val', 'date', 'Condition']

(sns.FacetGrid(df, col='Year', col_wrap=1, height=5)
    .map(sns.lineplot, *arg, hue_order=orders, palette=palette)
    .add_legend()
    .set_titles("{col_name}")
)

Upvotes: 1

Quang Hoang
Quang Hoang

Reputation: 150735

You can pass a hue_order to map:

orders = ['Poor', 'Average', 'Good']

arg = ['val', 'date', 'Condition']

(sns.FacetGrid(df, col='Year', col_wrap=1, height=5)
    .map(sns.lineplot, *arg, hue_order=orders)
    .add_legend()
    .set_titles("{col_name}")
)

Upvotes: 1

Related Questions