Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a plotly grouped bar chart using two different columns from spreadsheet?

I'm doing a side project and I'm having trouble making a grouped bar chart that displays in-state-tuition and out-of-state tuition. I'm only able to display one or the other. How can I make it display both side by side for each college?

import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
import plotly.io as pio

df = pd.read_csv('college_data.csv')

barchart = px.bar(
    data_frame = df,
    x = "college_name",
    #color = "out-of-state_tuition",
    y = "in-state_tuition",
    opacity = 0.9,
    orientation = "v",
    barmode = 'group',
    title='Annual In-State Tuition vs Out-of-state Tuition',
)
pio.show(barchart)

Bar chart using only one column: enter image description here

Spreadsheet with 2 different columns: enter image description here

like image 908
QuarterlyInteresting Avatar asked Sep 13 '25 02:09

QuarterlyInteresting


1 Answers

  • sample data as images and incomplete as columns makes in unusable in an answer. Hence have synthesized.
  • https://plotly.com/python-api-reference/generated/plotly.express.bar.html Either x or y can optionally be a list of column references or array_likes, in which case the data will be treated as if it were ‘wide’ rather than ‘long’.
import numpy as np
df = pd.DataFrame({"college_name":list("ABCD"),"in-state_tuition":np.random.randint(5000,7000,4),"out-of-state_tuition":np.random.randint(8000,15000,4)})

px.bar(
    data_frame = df,
    x = "college_name",
    y = ["in-state_tuition","out-of-state_tuition"],
    opacity = 0.9,
    orientation = "v",
    barmode = 'group',
    title='Annual In-State Tuition vs Out-of-state Tuition',
)

enter image description here

like image 83
Rob Raymond Avatar answered Sep 14 '25 17:09

Rob Raymond