Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a plotly bar graph with the correct value count with python?

data looks like this

A label
string negative
string negative
string negative
string positive
string positive
string negative
string positive

I want to make a simple plotly bar chart showing two bars - the count of positive and the count of negative, next to each other, blue and red.

If I do this

fig = px.bar(df, x=df["label"])

then I get this (btw do you know why the colors are muted out of nowhere?):

enter image description here

When I hover it says "count = 1 " I want it to say the actual count.. and I want to make the negative bar red. How do I do that?

like image 282
Domi Avatar asked May 13 '26 02:05

Domi


1 Answers

plotly has histogram chart type. https://plotly.com/python/histograms/

import io
import pandas as pd
import plotly.express as px

df = pd.read_csv(io.StringIO("""A,label
string,negative
string,negative
string,negative
string,positive
string,positive
string,negative
string,positive"""))

px.histogram(df, x="label", color="label", color_discrete_sequence=["red","blue"])

enter image description here

like image 140
Rob Raymond Avatar answered May 14 '26 14:05

Rob Raymond



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!