如何排序FacetGrid图例?

时间:2020-08-08 21:47:54

标签: python matplotlib seaborn facet-grid

在下图中,如何对图例进行排序以在“是”上方显示“否”?谢谢

import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt

df = sb.load_dataset('tips')
g = sb.FacetGrid(df, col='sex', hue='smoker', col_wrap=2)
g.map(plt.scatter, 'total_bill', 'tip', alpha=.7)
g.axes[-1].legend() 
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:2)

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

df = sns.load_dataset('tips')

# set smoker as categorical and ordered
df.smoker = pd.Categorical(df.smoker, categories=['No', 'Yes'], ordered=True)

# plot
g = sns.FacetGrid(df, col='sex', hue='smoker', height=5, col_wrap=2)
g.map(plt.scatter, 'total_bill', 'tip', alpha=.7)
g.axes[-1].legend() 
plt.show()

enter image description here