Altair wrong results

Viewed 34

I have created a graph but the numbers are not correct when I compare the dataset to the actual graph. The y left axis which is "Gross Profit" does not match the dataset. I have tried to search for similar cases but did not find anything. The y right axis seems to match the information I have in my dataset

data:

chica = {'Year': [2022, 2023, 2024, 2025, 2026, 
                  2022, 2023, 2024, 2025, 2026,
                  2022, 2023, 2024, 2025, 2026],
        'Investment': ['Low', 'Low', 'Low', 'Low', 'Low', 
                       'Medium','Medium','Medium','Medium','Medium', 
                       'High','High','High','High','High'],
        'Customers': [902, 1804, 2706, 3608, 4510, 
                      1077, 2154, 3231,4308,5385, 
                      1346, 2692, 4038, 5388, 6730],
        'Gross Profit': [131475, 262950, 394425, 525900, 657375, 
                         94845, 189690, 284535, 379380, 474225,
                         38140, 76280, 114420, 152560, 190700]}

df3 = pd.DataFrame(chica)
df3

attempt:

base = alt.Chart(df3).encode(
x=alt.X('Year:O', title= 'Year',axis=alt.Axis(labelAngle=325))
)
#Add line
line = base.mark_line(color='#55F546').encode(
y=alt.Y('Customers',title='Customers', axis=alt.Axis(grid=True),),
strokeDash ='Investment',
)
#Add background
bar = base.mark_bar(color='Investment').encode(
y='Gross Profit',
color = alt.Color('Investment', scale=alt.Scale(scheme = 'set1')),
)
#configure graph (size & colors)
de = (bar + line ).resolve_scale(y='independent').properties(title= 'Low, Medium and High Investment')
d= de.configure_title(fontSize=14).configure(background='white')
d.configure_axisLeft(
labelColor='red',
titleColor='red',
labelFontSize=15,
titleFontSize=15
).configure_axisRight(
labelColor='blue',
titleColor='blue',
labelFontSize=15,
titleFontSize=15
).configure_axisBottom(
labelColor='black',
titleColor='black',
labelFontSize=13,
titleFontSize=13
).configure_legend(
labelColor='black',
titleColor='black',
labelFontSize=16,
titleFontSize=16
).properties(
    width=500,
    height=350
)
1 Answers

After running you code, I get the following graph:

enter image description here

The left y-axis matches the data perfectly:

df3.groupby('Year')['Gross Profit'].sum()
Year
2022     264460
2023     528920
2024     793380
2025    1057840
2026    1322300
Name: Gross Profit, dtype: int64
Related