You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Altair图表左Y轴(Gross Profit)数据不符问题求助

Altair双轴图表左侧Y轴数据不匹配问题修复

问题现象

使用Altair创建双轴图表后,发现左侧Y轴“Gross Profit”的数值与数据集不一致,但右侧Y轴“Customers”数据完全匹配。

数据集

import pandas as pd

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)

原代码

import altair as alt

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
)

问题原因

核心问题在于柱状图的Y轴数据被默认聚合:

  • 每个年份对应3条不同Investment的数据,Altair默认会对同一年份的Gross Profit进行求和聚合,导致柱子高度是三个值的总和,和原始数据集的单个值不符。
  • 折线图因为指定了strokeDash='Investment',会按Investment分组绘制独立折线,每个数据点都被保留,所以和原始数据一致。

修复方案

修改柱状图的编码,确保每个(Year, Investment)组合的Gross Profit以独立柱子显示,且不进行聚合:

  1. 给柱状图添加xOffset='Investment',让同一年份的不同Investment柱子并列展示。
  2. 明确指定Y轴不使用聚合,设置y=alt.Y('Gross Profit:Q', aggregate=None)。

修复后的完整代码

import altair as alt
import pandas as pd

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)

base = alt.Chart(df3).encode(
    x=alt.X('Year:O', title='Year', axis=alt.Axis(labelAngle=325))
)

# 折线图部分不变
line = base.mark_line(color='#55F546').encode(
    y=alt.Y('Customers', title='Customers', axis=alt.Axis(grid=True)),
    strokeDash='Investment',
)

# 修改后的柱状图部分
bar = base.mark_bar().encode(
    # 明确指定不聚合,使用原始值
    y=alt.Y('Gross Profit:Q', title='Gross Profit', aggregate=None),
    color=alt.Color('Investment', scale=alt.Scale(scheme='set1')),
    # 添加xOffset实现同一年份的柱子并列
    xOffset='Investment'
)

# 图表组合与配置不变
de = (bar + line).resolve_scale(y='independent').properties(title='Low, Medium and High Investment')
d = de.configure_title(fontSize=14).configure(background='white')
final_chart = 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
)

final_chart

验证说明

修复后,左侧Y轴的Gross Profit数值会和数据集里的单个值完全匹配,同一年份下的Low/Medium/High三个投资级别的柱子会并列显示,折线图的Customers数据保持正常。

内容的提问来源于stack exchange,提问作者Leonardo Urbiola

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 04:26:03