seaborn boxplots at desired distances along the x

2020-01-31 10:41发布

Is there anyway to place seaborn boxplots at desired distances along the x axis?

I've got a dataframe with hierarchical column index with indices Assignment, Max, Type a row index of student names

+------------+----------+---------+----------+---------------+
| Type       | Homework | Quiz    | Homework | Presentations |
|            | max 100  | max 100 | max 100  | max 100       |
+------------+----------+---------+----------+---------------+
| Assignment | 1        | 2       | 3        | 4             |
+------------+----------+---------+----------+---------------+
| Student 1  | 88       | 98      | 100      | 85            |
+------------+----------+---------+----------+---------------+
| Student 2  | 96       | 79      | 100      | 97            |
+------------+----------+---------+----------+---------------+
| Student 3  | 87       | 79      | 72       | 78            |
+------------+----------+---------+----------+---------------+
| Student 4  | 87       | 84      | 90       | 85            |
+------------+----------+---------+----------+---------------+
| Student 5  | 73       | 91      | 76       | 90            |
+------------+----------+---------+----------+---------------+
| Student 6  | 70       | 75      | 98       | 82            |
+------------+----------+---------+----------+---------------+
| Student 7  | 85       | 71      | 73       | 75            |
+------------+----------+---------+----------+---------------+
| Student 8  | 76       | 81      | 94       | 86            |
+------------+----------+---------+----------+---------------+
| Student 9  | 97       | 80      | 95       | 88            |
+------------+----------+---------+----------+---------------+

In reality the Assignments are strings and more descriptive.

I can easily feed the dataframe into seaborn and it will produce a nice box plot sns.boxplot(df)

What I'd really like is for the boxes to be separated onto different subplots (not hard), but to be spaced properly chronologically.

More clearly:

Currently sns.boxplot(df) places all the box plots chronologically which is nice. I'd like a subplot above it, for example, which had only the quiz box plots, but the quiz box plots are lined up horizontally on the x axis with where they would fall if all the assignments were included.

Is there anyway to place seaborn boxplots at desired distances along the x axis?

sns.boxplot(df['Quiz'], x=[1,5,9,12]) DOES NOT work as you can't override the x 'values' (but these are just the labels).

2条回答
混吃等死
2楼-- · 2020-01-31 11:35
import numpy as np
import pandas as pd
import seaborn as sns
df = pd.DataFrame(dict(x=np.repeat([0, 3, 5, 6], 10),
                       y=np.random.randn(40)))
sns.boxplot(x="x", y="y", data=df, order=np.arange(7))

enter image description here

查看更多
劫难
3楼-- · 2020-01-31 11:47

The short answer is no, seaborn.boxplot does not have an option to specify the position of the boxplots along the x axis.

If you don't care much for the style or can specify it manually, you can use pandas.DataFrame.boxplot instead, which has positions property.

import matplotlib.pyplot as plt
import pandas as pd
import numpy.random as rnd   # just to generate some data

data = pd.DataFrame(rnd.randn(10,4))
data.boxplot(positions=[1,5,6,10])
plt.grid('off')

boxplot with positions

UPDATE: It seems, I was wrong, as user mwaskom pointed out, you can specify positions by cleverly using order keyword, but it seems you would need to reshape your data from 'wide' to 'long' format.

查看更多
登录 后发表回答