简体   繁体   中英

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.

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)

图片

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", ie 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:

有序箱线图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM