Grouped Bar plot with columns as x-axis and values in Y-axis

I have the follwing data

m =
{ ‘Names’ : [‘Tom’, ‘Nick’, ‘George’, ‘Dan’],
‘English’: [20, 35, 40, 52],
‘Maths’ : [27, 42, 33, 73],
‘Science’ : [82, 61, 55, 23]
}

df = pd.DataFrame(data = m)
df

||Names|English|Maths|Science|
| — | — | — | — | — |
|0|Tom|20|27|82|
|1|Nick|35|42|61|
|2|George|40|33|55|
|3|Dan|52|73|23|

I want to make a grouped barplot where

X-axis ticks/labels are the titles : English, Maths, Science
Y-axis - correponds to the value
and Names corresponds to the legend & color

Thank you!!!

Welcome to the forums, @rajpmehta

Did you try to create the graph?

import plotly.express as px
import pandas as pd

m ={ 'Names' : ['Tom', 'Nick', 'George', 'Dan'],
'English': [20, 35, 40, 52],
'Maths' : [27, 42, 33, 73],
'Science' : [82, 61, 55, 23]
}

# create df, use names as index
df = pd.DataFrame(
    data= m, 
    index=m['Names']
)

# delete 'Names' column
df = df.drop('Names', axis=1)

# transpose df
df = df.transpose()

# create figure
fig = px.bar(df, x=df.index, y=df.columns, barmode='group')

# hide axis title
fig.update_xaxes(title='')
fig.show()

Thank you Aimped,

I did try the transpose option, however, I was wondering if there was someway to avoid doing that (?), as I noticed that it messes up the column heading after transpose and if I were to have 100+ columns to transpose, maybe it would be a bit tiring and not efficient to run the transpose.

I think this might be the only way to do it with plotly.express.

If you don’t want to change the df, I think you will have to do the grouping opeartions which px does under the hood yourself and create the traces, colors and so on using plotly.graph_objects. IMO this would be “worse” than transposing the df.

But maybe someone else hase a good idea for this :raised_hands:

I understand, was just curious if there was any alternate way to go about it. Thank you very much!

As an extension, I need help with colors here. For each group, I want the max to be ‘red’ and rest ‘grey’.
I naturally think of a ‘if’ fucntion, but don’t know how to execute it, across all groups

You initially stated that you want the colors to correspond to the names.

Not sure I understand you new color requirement.

That’s correct and that was solved.
Later, however upon further thinking, I thought it would be nice to highlight just the max value color for each subject, to allow focus on that and rest to be grey.

PS. Apologies for really late response.