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

如何在Vega Lite中配置热力图颜色编码?数值全显0问题求助

问题解决:英超比分热力图数值全为0的修复

问题根源

  • 轴类型设置错误:FTHG(主队进球)、FTAG(客队进球)是离散整数值,你用了quantitative(连续数值)类型,导致数据无法按整数比分正确分组,聚合计数失效。
  • 颜色编码配置冗余:color字段同时指定aggregate: "count"和field: "__count"是多余的,Vega-Lite在使用count聚合时会自动生成计数字段,额外的field配置会干扰聚合逻辑。

修正后的代码

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.2.0.json",
  "title": {
    "text": "Frequency of scorelines Premier League, 2016-17",
    "subtitle": ["Source: Football Datahub"],
    "subtitleFontStyle": "italic",
    "subtitleFontSize": 10,
    "anchor": "start",
    "color": "black"
  },
  "data": {
    "url": "https://pkgstore.datahub.io/sports-data/english-premier-league/season-1617_csv/data/d6b7551d3e130b6e59240d7018524498/season-1617_csv.csv"},
  "mark": "rect",
  "height": 300,
  "width": 300,
  "encoding": {
    "x": {
      "field": "FTHG",
      "type": "ordinal",
      "title": "",
      "axis": {"format": ".0f"}
    },
    "y": {
      "field": "FTAG",
      "type": "ordinal",
      "title": ""
    },
    "color": {
      "aggregate": "count",
      "type": "quantitative",
      "scale": {"scheme": "inferno"}
    }
  },
  "config": {
    "view": {
      "stroke": "transparent"
    }
  }
}

关键修改点

  1. 将x/y轴的type从quantitative改为ordinal,确保按整数比分分组;
  2. 删除color编码中的field: "__count",保留aggregate: "count"即可正确生成计数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 22:10:35