marimo-team / marimo

A reactive notebook for Python — run reproducible experiments, execute as a script, deploy as an app, and version with git.
https://marimo.io
Apache License 2.0
5.39k stars 158 forks source link

marimo altair_chart not displaying log scaled altair chart correctly #1597

Open andrewhill157 opened 3 weeks ago

andrewhill157 commented 3 weeks ago

Describe the bug

In the example below, the altair chart with passed to mo.ui.altair_chart does not appear to display correctly when the axis is log scaled (not sure if this applies to any other scales)?

image

(note that I had killed my marimo process when took the screenshot, which is why background is red)

Environment

{ "marimo": "0.6.17", "OS": "Darwin", "OS Version": "23.5.0", "Processor": "arm", "Python Version": "3.10.4", "Binaries": { "Browser": "--", "Node": "v21.7.1" }, "Requirements": { "click": "8.1.7", "importlib-resources": "missing", "jedi": "0.19.1", "markdown": "3.5.2", "pymdown-extensions": "10.7", "pygments": "2.17.2", "tomlkit": "0.12.3", "uvicorn": "0.29.0", "starlette": "0.37.2", "websockets": "12.0", "typing-extensions": "4.9.0", "black": "23.12.1" } }

Code to reproduce

import marimo as mo
import polars as pl
import altair as alt
import pandas as pd

def test(scale_type):
    data = {
        'x': range(1, 11),
        'y': [5, 10, 2, 8, 3, 15, 1, 4, 9, 6]
    }
    df = pl.DataFrame(data)

    # return chart with specified scale type for Y
    return (
        alt.Chart(df.to_pandas())
        .mark_bar(stroke="black", strokeWidth=0.5)
        .encode(
            x=alt.X(
                "x",
            ),
            y=alt.Y(
                "y",
                scale=alt.Scale(type=scale_type),
            ),
        )
    )

    # Display the chart
    return chart

linear = test("linear")
log = test("log")
sqrt = test("sqrt")

# Display the altair and mo.ui.altair_chart versions alongside one another
mo.hstack([mo.vstack([linear, log, sqrt]), mo.vstack([mo.ui.altair_chart(linear), mo.ui.altair_chart(log), mo.ui.altair_chart(sqrt)])])
mscolnick commented 3 weeks ago

Thanks for the easy-to-follow repro. Another interesting bit, when I export both log scales (altair and mo.ui.altair_chart) to the vega editor, neither work:

Altair

mo.ui.altair

mscolnick commented 3 weeks ago

Still unsure why this fails (even in the vega editor), but symlog seems more reliable than log as a scale setting.

mscolnick commented 1 week ago

Filed two issues to: https://github.com/vega/altair/issues/3447 https://github.com/vega/vega-lite/issues/9373

andrewhill157 commented 5 days ago

Thank you for filing!