简体   繁体   中英

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?):

在此处输入图片说明

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?

执行groupby并使用count进行聚合以获得一个新的 DataFrame ,其中包含label下两个类的计数:

fig = px.bar(df.groupby([‘a’]).count(),x=‘label’,color =‘label’, color_discrete_sequence=[‘blue’,’red’])

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"])

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM