如何对 FacetGrid 图例进行排序?
How to sort a FacetGrid legend?
在下图中,如何对图例进行排序以在 'Yes' 上方显示 'No'?谢谢
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()
- 将
'smoker'
列设置为分类 pandas.Categorical
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()
在下图中,如何对图例进行排序以在 'Yes' 上方显示 'No'?谢谢
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()
- 将
'smoker'
列设置为分类pandas.Categorical
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()