Issue
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
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:
Answered By - bbkglb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.