Vega/Altair 图中的条形图未延伸至完整尺寸

问题描述 投票:0回答:1

我正在尝试使用 Vega-lite(实际上是 Python 中的 Altair,但直接使用 Vega 时也遇到了问题)根据预分箱数据绘制直方图。我的数据集包含以下字段:

  • bin:bin ID
  • center_point:垃圾箱的中心
  • left_bound:垃圾箱的开头
  • right_bound:垃圾箱的末端。
  • n:直方图的计数。

我使用以下 Python 代码来绘制图表:

chart = alt.Chart(data).mark_bar().encode(
    x = "left_bound",
    x2 = "right_bound",
    y = "n"
)

得到以下结果:

enter image description here

我不明白为什么酒吧没有一路下降。仅当我指定 x2 时才会发生这种情况。如果我摆脱它,酒吧就会开始一路下降。

chart = alt.Chart(data).mark_bar().encode(
    x = "left_bound",
    y = "n"
)

enter image description here

但是,这样做不是一个选择,因为垃圾箱的宽度可能不相等。可能出了什么问题?

下面是 JSON 格式的完整 Vega-lite 规范,以及包含此规范的 VegaEditor 的链接。

在 Vega 编辑器中打开图表

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.17.0.json",
  "config": {"view": {"continuousHeight": 300, "continuousWidth": 500}},
  "data": {"name": "data-303fbb5af18e896de75409a7bbd56ac0"},
  "datasets": {
    "data-303fbb5af18e896de75409a7bbd56ac0": [
      {
        "bin": 0,
        "center_point": -1.95,
        "left_bound": -2,
        "n": 3401,
        "right_bound": -1.9
      },
      {
        "bin": 1,
        "center_point": -1.85,
        "left_bound": -1.9,
        "n": 3945,
        "right_bound": -1.8
      },
      {
        "bin": 2,
        "center_point": -1.75,
        "left_bound": -1.8,
        "n": 4142,
        "right_bound": -1.7
      },
      {
        "bin": 3,
        "center_point": -1.65,
        "left_bound": -1.7,
        "n": 4843,
        "right_bound": -1.6
      },
      {
        "bin": 4,
        "center_point": -1.55,
        "left_bound": -1.6,
        "n": 5440,
        "right_bound": -1.5
      },
      {
        "bin": 5,
        "center_point": -1.45,
        "left_bound": -1.5,
        "n": 5847,
        "right_bound": -1.4
      },
      {
        "bin": 6,
        "center_point": -1.35,
        "left_bound": -1.4,
        "n": 6826,
        "right_bound": -1.3
      },
      {
        "bin": 7,
        "center_point": -1.25,
        "left_bound": -1.3,
        "n": 7764,
        "right_bound": -1.2
      },
      {
        "bin": 8,
        "center_point": -1.15,
        "left_bound": -1.2,
        "n": 9205,
        "right_bound": -1.1
      },
      {
        "bin": 9,
        "center_point": -1.05,
        "left_bound": -1.1,
        "n": 11149,
        "right_bound": -1
      },
      {
        "bin": 10,
        "center_point": -0.95,
        "left_bound": -1,
        "n": 13726,
        "right_bound": -0.9
      },
      {
        "bin": 11,
        "center_point": -0.85,
        "left_bound": -0.9,
        "n": 17569,
        "right_bound": -0.8
      },
      {
        "bin": 12,
        "center_point": -0.75,
        "left_bound": -0.8,
        "n": 22626,
        "right_bound": -0.7
      },
      {
        "bin": 13,
        "center_point": -0.65,
        "left_bound": -0.7,
        "n": 30866,
        "right_bound": -0.6
      },
      {
        "bin": 14,
        "center_point": -0.55,
        "left_bound": -0.6,
        "n": 45078,
        "right_bound": -0.5
      },
      {
        "bin": 15,
        "center_point": -0.45,
        "left_bound": -0.5,
        "n": 72752,
        "right_bound": -0.4
      },
      {
        "bin": 16,
        "center_point": -0.35,
        "left_bound": -0.4,
        "n": 124899,
        "right_bound": -0.3
      },
      {
        "bin": 17,
        "center_point": -0.25,
        "left_bound": -0.3,
        "n": 223861,
        "right_bound": -0.2
      },
      {
        "bin": 18,
        "center_point": -0.15,
        "left_bound": -0.2,
        "n": 416260,
        "right_bound": -0.1
      },
      {
        "bin": 19,
        "center_point": -0.05,
        "left_bound": -0.1,
        "n": 1125814,
        "right_bound": 0
      },
      {
        "bin": 20,
        "center_point": 0.05,
        "left_bound": 0,
        "n": 445494,
        "right_bound": 0.1
      },
      {
        "bin": 21,
        "center_point": 0.15,
        "left_bound": 0.1,
        "n": 386848,
        "right_bound": 0.2
      },
      {
        "bin": 22,
        "center_point": 0.25,
        "left_bound": 0.2,
        "n": 398402,
        "right_bound": 0.3
      },
      {
        "bin": 23,
        "center_point": 0.35,
        "left_bound": 0.3,
        "n": 440893,
        "right_bound": 0.4
      },
      {
        "bin": 24,
        "center_point": 0.45,
        "left_bound": 0.4,
        "n": 537249,
        "right_bound": 0.5
      },
      {
        "bin": 25,
        "center_point": 0.55,
        "left_bound": 0.5,
        "n": 611479,
        "right_bound": 0.6
      },
      {
        "bin": 26,
        "center_point": 0.65,
        "left_bound": 0.6,
        "n": 678911,
        "right_bound": 0.7
      },
      {
        "bin": 27,
        "center_point": 0.75,
        "left_bound": 0.7,
        "n": 764600,
        "right_bound": 0.8
      },
      {
        "bin": 28,
        "center_point": 0.85,
        "left_bound": 0.8,
        "n": 927396,
        "right_bound": 0.9
      },
      {
        "bin": 29,
        "center_point": 0.95,
        "left_bound": 0.9,
        "n": 4172231,
        "right_bound": 1
      }
    ]
  },
  "encoding": {
    "x": {"field": "left_bound", "type": "quantitative"},
    "x2": {"field": "right_bound"},
    "y": {"field": "n", "type": "quantitative"}
  },
  "mark": {"type": "bar"}
}
python vega-lite altair
1个回答
0
投票

当存在

y2
编码时,条形图似乎还需要指定
x2
编码。不确定为什么会这样,但这有效:

  "encoding": {
    "x": {"field": "left_bound", "type": "quantitative"},
    "x2": {"field": "right_bound"},
    "y": {"field": "n", "type": "quantitative"},
    "y2": {"datum": 0}
  },
  "mark": {"type": "bar"}

您也可以使用

mark_rect
代替,它看起来不需要
y2
编码:

  "encoding": {
    "x": {"field": "left_bound", "type": "quantitative"},
    "x2": {"field": "right_bound"},
    "y": {"field": "n", "type": "quantitative"}
  },
  "mark": {"type": "rect"}
© www.soinside.com 2019 - 2024. All rights reserved.