如何在 seaborn boxplot 中对情节进行排名
How to rank plot in seaborn boxplot
以下面的seaborn箱线图为例,来自https://stanford.edu/~mwaskom/software/seaborn/examples/horizontal_boxplot.html
import numpy as np
import seaborn as sns
sns.set(style="ticks", palette="muted", color_codes=True)
# Load the example planets dataset
planets = sns.load_dataset("planets")
# Plot the orbital period with horizontal boxes
ax = sns.boxplot(x="distance", y="method", data=planets,
whis=np.inf, color="c")
# Add in points to show each observation
sns.stripplot(x="distance", y="method", data=planets,
jitter=True, size=3, color=".3", linewidth=0)
# Make the quantitative axis logarithmic
ax.set_xscale("log")
sns.despine(trim=True)
是否可以 "rank" 条目,从大到小(或反之亦然)?在此图中,"astrometry" 应该是最后一个条目,如果从大到小排列。
您可以在 sns.boxplot
和 sns.stripplot
函数中使用 order
参数来订购您的 "boxes"。这是一个如何执行此操作的示例(因为不完全清楚 "greatest to lowest" 的意思,即您要根据哪个变量对条目进行排序,我假设您想根据总和对它们进行排序在它们的 "distance" 值中,如果您想根据不同的值对它们进行排序,您应该能够编辑解决方案以满足您的需要):
import numpy as np
import seaborn as sns
sns.set(style="ticks", palette="muted", color_codes=True)
# Load the example planets dataset
planets = sns.load_dataset("planets")
# Determine the order of boxes
order = planets.groupby(by=["method"])["distance"].sum().iloc[::-1].index
# Plot the orbital period with horizontal boxes
ax = sns.boxplot(x="distance", y="method", data=planets,
order=order, whis=np.inf, color="c")
# Add in points to show each observation
sns.stripplot(x="distance", y="method", data=planets, order=order,
jitter=True, size=3, color=".3", linewidth=0)
# Make the quantitative axis logarithmic
ax.set_xscale("log")
sns.despine(trim=True)
修改后的代码(注意 sns.boxplot
和 sns.stripplot
函数中 order
参数的使用)将生成下图:
试试这个:
import numpy as np
import seaborn as sns
sns.set(style="ticks", palette="muted", color_codes=True)
# Load the example planets dataset
planets = sns.load_dataset("planets")
ranks = planets.groupby("method")["distance"].mean().fillna(0).sort_values()[::-1].index
# Plot the orbital period with horizontal boxes
ax = sns.boxplot(x="distance", y="method", data=planets,
whis=np.inf, color="c", order = ranks)
# Add in points to show each observation
sns.stripplot(x="distance", y="method", data=planets,
jitter=True, size=3, color=".3", linewidth=0, order = ranks)
# Make the quantitative axis logarithmic
ax.set_xscale("log")
sns.despine(trim=True)
以下面的seaborn箱线图为例,来自https://stanford.edu/~mwaskom/software/seaborn/examples/horizontal_boxplot.html
import numpy as np
import seaborn as sns
sns.set(style="ticks", palette="muted", color_codes=True)
# Load the example planets dataset
planets = sns.load_dataset("planets")
# Plot the orbital period with horizontal boxes
ax = sns.boxplot(x="distance", y="method", data=planets,
whis=np.inf, color="c")
# Add in points to show each observation
sns.stripplot(x="distance", y="method", data=planets,
jitter=True, size=3, color=".3", linewidth=0)
# Make the quantitative axis logarithmic
ax.set_xscale("log")
sns.despine(trim=True)
是否可以 "rank" 条目,从大到小(或反之亦然)?在此图中,"astrometry" 应该是最后一个条目,如果从大到小排列。
您可以在 sns.boxplot
和 sns.stripplot
函数中使用 order
参数来订购您的 "boxes"。这是一个如何执行此操作的示例(因为不完全清楚 "greatest to lowest" 的意思,即您要根据哪个变量对条目进行排序,我假设您想根据总和对它们进行排序在它们的 "distance" 值中,如果您想根据不同的值对它们进行排序,您应该能够编辑解决方案以满足您的需要):
import numpy as np
import seaborn as sns
sns.set(style="ticks", palette="muted", color_codes=True)
# Load the example planets dataset
planets = sns.load_dataset("planets")
# Determine the order of boxes
order = planets.groupby(by=["method"])["distance"].sum().iloc[::-1].index
# Plot the orbital period with horizontal boxes
ax = sns.boxplot(x="distance", y="method", data=planets,
order=order, whis=np.inf, color="c")
# Add in points to show each observation
sns.stripplot(x="distance", y="method", data=planets, order=order,
jitter=True, size=3, color=".3", linewidth=0)
# Make the quantitative axis logarithmic
ax.set_xscale("log")
sns.despine(trim=True)
修改后的代码(注意 sns.boxplot
和 sns.stripplot
函数中 order
参数的使用)将生成下图:
试试这个:
import numpy as np
import seaborn as sns
sns.set(style="ticks", palette="muted", color_codes=True)
# Load the example planets dataset
planets = sns.load_dataset("planets")
ranks = planets.groupby("method")["distance"].mean().fillna(0).sort_values()[::-1].index
# Plot the orbital period with horizontal boxes
ax = sns.boxplot(x="distance", y="method", data=planets,
whis=np.inf, color="c", order = ranks)
# Add in points to show each observation
sns.stripplot(x="distance", y="method", data=planets,
jitter=True, size=3, color=".3", linewidth=0, order = ranks)
# Make the quantitative axis logarithmic
ax.set_xscale("log")
sns.despine(trim=True)