Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

python - seaborn boxplots at desired distances along the x axis

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...