vega / vega-lite

A concise grammar of interactive graphics, built on Vega.
https://vega.github.io/vega-lite/
BSD 3-Clause "New" or "Revised" License
4.65k stars 606 forks source link

Logarithmic scale not showing any results #9373

Open mscolnick opened 3 months ago

mscolnick commented 3 months ago

Bug Description

Logarithmic scale not showing any results. This works when removing "scale" or changing to "symlog".

{
  "config": {"view": {"continuousWidth": 300, "continuousHeight": 300}},
  "data": {"name": "data"},
  "mark": {"type": "bar", "stroke": "black", "strokeWidth": 0.5},
  "encoding": {
    "x": {"field": "x", "type": "quantitative"},
    "y": {"field": "y", "scale": {"type": "log"}, "type": "quantitative"}
  },
  "$schema": "https://vega.github.io/schema/vega-lite/v5.17.0.json",
  "datasets": {
    "data": [
      {"x": 1, "y": 5},
      {"x": 2, "y": 10},
      {"x": 3, "y": 2},
      {"x": 4, "y": 8},
      {"x": 5, "y": 3},
      {"x": 6, "y": 15},
      {"x": 7, "y": 1},
      {"x": 8, "y": 4},
      {"x": 9, "y": 9},
      {"x": 10, "y": 6}
    ]
  }
}

image

https://vega.github.io/editor/#/url/vega-lite/N4Igxg9gdgZglgcxALlANzgUwO4tJKAFzigFcJSBnAdTgBNCALFAZgAY2AacaYsiygAlMiRoVYcAvpO50AhoTl4QUOQFtMKEPMUgZINXIBOAa2WEAngAdNyEACNjIbpUJGIJ2w4A2csGZc3D0xaBmZkNgA6AFZ9TChIOhIkVBAAD2V4TG86LQzuSxstAEdSOSI4RWI0TX0LTKwcrXqXMDlvW1BCr28IJH1ukrKKqrgavX0AEkowRkxDLTFCK0pkAHo1moQ5SIRKxlJ7SLgINZm5w03MbYBab0rMTejIgEYAdkiogCtKaGdtBRySiYQiUZQ6JTIADaoAyyBe3HqyFinFhKAATIiUC82DI0cgWFjkOi8ekUAAWIkADlJcOiRJYtJQADYiS8Ufi3mymcgqUTyTyAJxEwU8nFE5mSAC60iAA

Checklist

kgoodrick-uu commented 3 months ago

By default, the bar chart draws a bar from 0 to the y encoding value. It's not possible to include 0 in a logarithmic scale, so it does not draw anything. You can fix this by giving vega-lite an alternative starting location for the bar that can be represented on a logarithmic scale. You can do this by providing a constant "y2" encoding e.g.

"y2": {"datum": 0.1}

image

Working example here.

mscolnick commented 3 months ago

Thanks for explaining. But in my example, there is no datum with a y value of 0. Am I missing something?

kgoodrick-uu commented 3 months ago

Hi @mscolnick, the 0 is implied with bar mark which is generally desirable as almost all bar charts go from 0 to the value. Since this is not possible with a logarithmic scale you have to tell vega-lite what you want it to do by manually setting a datum that can be represented on a log scale.

mscolnick commented 3 months ago

Got it - thanks for clarifying.

What's weird is that when Altair renders this, it works, but the "Open in Vega Editor" still does not work. (p.s. I will file this oddity in the Altair github)

For example, here is a reproduction: https://marimo.app/l/nhwpi8

import altair as alt
import pandas as pd
data = {"x": range(1, 11), "y": [5, 10, 2, 8, 3, 15, 1, 4, 9, 6]}
df = pd.DataFrame(data)

(
    alt.Chart(df)
    .mark_bar(stroke="black", strokeWidth=0.5)
    .encode(
        x=alt.X(
            "x",
        ),
        y=alt.Y(
            "y",
            scale=alt.Scale(type="log"),
        ),
    )
)
image

But the produced vega playground, that can be found here, still does not render (due to the y=0 issue mentioned above).

PBI-David commented 3 months ago

For some reason, the same spec in the online editor introduces a stack transform in the compiled Vega which is what is causing the issue. Maybe one of the devs will know why.

mscolnick commented 3 months ago

Thank you - I filed an issue in the Altair repo: https://github.com/vega/altair/issues/3447

EDIT: They believe this (vega-lite) is the is the correct place for the issue

domoritz commented 3 months ago

What solution do you propose? Not implying automatic zero for bars when the scale is log?

mscolnick commented 2 months ago

@domoritz - i'm not familiar enough with vega-lite to propose a solution, but is this not a regression? is anything wrong with the previous implementation?