'How to rank plot in seaborn boxplot
Take the following seaborn boxplot for example, from 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)
Is it possible to "rank" the entries, from greatest to lowest (or vice versa)? In this plot, "astrometry" should be the last entry, if ranking from greatest to lowest.
Solution 1:[1]
You can use the order
argument in the sns.boxplot
and sns.stripplot
functions to order your "boxes". Here is an example of how to do this (since it is not completely clear what you mean by "greatest to lowest", i.e. which variable you want to sort the entries based on, I am assume you want to sort them based on the sum of their "distance" values, you should be able to edit the solution to fit your needs if you want to sort them based on a different value):
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)
This modifed code (notice the use of the order
argument in the sns.boxplot
and sns.stripplot
functions) will produce the following figure:
Solution 2:[2]
Try this:
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mwaskom |
Solution 2 |