1

I have a pandas dataframe for a highschool exam scores which stores a user's race (types are a, b,c,d) and their physics, math, and english scores. I want to create a single plot using seaborn plots that will show how a person's race corresponds to the AVERAGE math, chemstry, and physics scores? I attached an image below to the example data frame but it just doesn't have the race column but pretend it had a column for race and its types were (A,B,C,D).

enter image description here

Thought about using a barplot with the race categories on the x-axis but how do i have different bars for each average subject score in the same way 'hue' is used for sex in this example below enter image description here

Need to know if there's another another more suitable graph I can use? and how would i implement it

3
  • Can you provide an image for the desired output? Perhaps you meant to include an image from the seaborn.barplot documentation? I've edited the question with what I assume your desired output would be like Commented Dec 11, 2021 at 20:26
  • @DerekO - I'm not sure your substantial edit was warranted. The onus is on OP to provide clarification. Your interpretation may be altogether incorrect, and is also putting words in OP's mouth that weren't there. Please don't do this. If anything, you can suggest it in a comment, but editing it into the question is inappropriate. I've rolled back to before your substantial edit. Commented Dec 11, 2021 at 21:11
  • @BigBen okay that's fair point. i'll avoid being overly heavy handed with edits in future Commented Dec 11, 2021 at 22:04

1 Answer 1

4

You'll want to first melt the Chemistry, Physics, Math columns down into two columns: one identifer column and another column with the corresponding values.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

## recreate your DataFrame
df = pd.DataFrame({
    'Name':list("QWERTYUI"),
    'Student ID': list(range(12001,12009)),
    'Race':list('ABCDABCD'),
    'Chemistry': [59,54,42,66,60,78,64,82],
    'Physics': [90,42,88,48,80,73,43,69],
    'Math': [45,85,54,64,72,64,67,70]
})

df_melted = pd.melt(df, id_vars=['Name','Student ID','Race'], value_vars=['Chemistry','Physics','Math'])

Here you can see the difference between how the two DataFrames are structured:

>>> df
  Name  Student ID Race  Chemistry  Physics  Math
0    Q       12001    A         59       90    45
1    W       12002    B         54       42    85
2    E       12003    C         42       88    54
3    R       12004    D         66       48    64
4    T       12005    A         60       80    72
5    Y       12006    B         78       73    64
6    U       12007    C         64       43    67
7    I       12008    D         82       69    70

>>> df_melted
   Name  Student ID Race   variable  value
0     Q       12001    A  Chemistry     59
1     W       12002    B  Chemistry     54
2     E       12003    C  Chemistry     42
3     R       12004    D  Chemistry     66
4     T       12005    A  Chemistry     60
5     Y       12006    B  Chemistry     78
6     U       12007    C  Chemistry     64
7     I       12008    D  Chemistry     82
8     Q       12001    A    Physics     90
9     W       12002    B    Physics     42
10    E       12003    C    Physics     88
11    R       12004    D    Physics     48
12    T       12005    A    Physics     80
13    Y       12006    B    Physics     73
14    U       12007    C    Physics     43
15    I       12008    D    Physics     69
16    Q       12001    A       Math     45
17    W       12002    B       Math     85
18    E       12003    C       Math     54
19    R       12004    D       Math     64
20    T       12005    A       Math     72
21    Y       12006    B       Math     64
22    U       12007    C       Math     67
23    I       12008    D       Math     70

Then you can call sns.barplot with the following arguments:

ax = sns.barplot(x="Race", y="value", hue="variable", data=df_melted)
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.