Generate Bar chart with plotly express on Python

Hi everyone,
I am working on a big data file and wish to visualize it and whats better than Plotly for this. I like to plot a bar chart by using plotly express. I write piece of code for that the issue is that it’s not plotting (A,B)

One thing more that when I apply it on the big data beside of not plotting A&B column the plot color is very light
I don’t want to use plotly.graph_objs as go for creating barchart as it doesn’t perform mouse hovering.
Sample data:

CLASS,SCORE
1,385.45
1,424.66
2,1302.35
2,1501.93
3,1466.27
3,988.13
3,1432.21
3,200
3,750
4,1466.27
4,988.13
4,1432.21
5,1466.27
5,988.13
6,1432.21
6,1466.27
7,988.13
8,1432.21
8,1466.27
8,988.13
9,1432.21
10,1466.27
11,988.13
11,1432.21
12,1466.27
12,988.13
13,1432.21
A,617.39
A,1808.94
B,2507.65
B,2354.97

Script:

import pandas as pd
import plotly.express as px
df = pd.read_csv("data-3.csv")
fig = px.bar(df, x = 'CLASS', y = 'SCORE')
fig.show()

Hi @Flavia, welcome to the forum! First question: what are A and B :slight_smile: ? They can’t be seen in your data. Given the sample data you displayed and the code you shared using px.bar, the result looks good to me. Maybe you want to increase the opacity parameter if the bars look too transparent to you. Please give more details about what you would like to do.

One last thing: you should be able to get also hover information with graph_objects, but indeed plotly.express functions are convenient for one-liner instructions as the one you wrote.

@Emmanuelle Hi, Thankyou for the response can you please consider the last rows of sample data which contains A & B classes which are not plotting that’s the concern. Actually, I have tried to do it with graph object and it’s only plot the first score for each class which is not required that’s why I don’t want to use it. While express store all the scores associated with each class in a bar and also provide mouse hovering. Can you please help me with this how to plot A & B along with other classes??

12,988.13
13,1432.21
A,617.39
A,1808.94
B,2507.65
B,2354.97

ok thanks for the answer. You need to force the xaxis type to br category. I’m a bit surprised that it’s not done automatically, but setting it yourself does the job:

fig = px.bar(df, x='CLASS', y='SCORE')
fig.update_xaxes(type='category')
1 Like

The reason in your second plot that everything looks washed out is because there are many many little blue bars with grey outlines and the outlines are hiding the blue bars. You may want to use px.histogram to aggregate the data into a single bar, or pre-aggregate the data in your data frame.

1 Like

Ah I should add that I suspect the reason you have to force a category axis is because the heuristic that tries to set the type is seeing many more numbers than strings and so is forcing the axis to ‘linear’ instead of ‘category’.

1 Like

Instead of bar, I try to do px.scatter and it works fine.

Yeah. I tried px.scatter it works fine then. Thanks for the explanation!!