Home > other >  Python Plotly - graph issue with comparing difference between 2 bar graph values each day - error ON
Python Plotly - graph issue with comparing difference between 2 bar graph values each day - error ON

Time:07-08

I have two scores each day in a bar graph and I am denoting the difference between the score for each day. It works fine as is, but I want to exclude weekends. When I do this, I'm getting an error. I'm unsure what I am doing wrong, but any help is appreciated. I've also provided an image of the desired output - see: [1]: https://i.stack.imgur.com/7T5QU.png

import plotly.graph_objects as go
import math
import pandas as pd
import numpy as np

df1 = pd.DataFrame({
   'score': [11,42,31,22],
   'dates': ['7/1/2022','7/2/2022','7/3/2022','7/4/2022'],
   'weekend': [0,1,1,0],
})

df2 = pd.DataFrame({
   'score': [44,45,26,22],
   'dates': ['7/1/2022','7/2/2022','7/3/2022','7/4/2022'],
   'weekend': [0,1,1,0],
})

#df1 = df1[(df1['weekend'] == 0)] When I filter out weekends this doesn't work, otherwise it's fine
#df2 = df2[(df2['weekend'] == 0)]

y1 = df1['score']
y2 = df2['score']

c = []
i = 0

while i < len(y1):
    i = i
    q = y1[i]-y2[i] 
    c.append(str(q))
    i=i 1

fig = go.Figure(data=[
    go.Bar(name='SF Zoo', x=df1['dates'], y=y1),
    go.Bar(name='LA Zoo', x=df2['dates'], y=y2, text=c, textposition='outside')
])


fig.update_layout(barmode='group')
fig.show()


CodePudding user response:

You have a few options to return the subset of values in y1 and y2 in an array-like format after filtering out weekends. Update your code with either of these options:

Option 1:

y1 = df1['score'].values
y2 = df2['score'].values

Option 2:

y1 = df1['score'].to_numpy()
y2 = df2['score'].to_numpy()

Option 3:

y1 = df1['score'].tolist()
y2 = df2['score'].tolist()
  • Related