例如,从https://stanford.edu/~mwaskom/software/seaborn/examples/horizontal_boxplot.html
获取以下seaborn boxplotimport 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)
是否可以对条目进行“排名”,从最高到最低(反之亦然)?在这个图中,如果排名从最高到最低,“天体测量”应该是最后一个条目。
答案 0 :(得分:3)
试试这个:
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)
答案 1 :(得分:1)
您可以使用order
和sns.boxplot
函数中的sns.stripplot
参数来订购“框”。下面是一个如何执行此操作的示例(因为“最大到最低”并不完全清楚您的意思,即您希望根据条目对条目进行排序,我假设您要根据总和对它们进行排序对于他们的“距离”值,如果你想根据不同的值对它们进行排序,你应该能够编辑解决方案以满足你的需求):
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)
这个修改过的代码(注意order
和sns.boxplot
函数中sns.stripplot
参数的使用将产生下图: