Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for resolve_scale to work with latitude and longitude #9509

Closed
thomascamminady opened this issue Jan 6, 2025 · 3 comments
Closed

Allow for resolve_scale to work with latitude and longitude #9509

thomascamminady opened this issue Jan 6, 2025 · 3 comments

Comments

@thomascamminady
Copy link

I came across this when trying to plot the running tracks I ran this year. For each run, I have latitude and longitude data and I want to plot the track. Each column should be a new route. And since tracks happen at various places across many countries, I need the scale to be resolved independently.

Here's an MWE in altair that works:

import altair as alt
import polars as pl

base = (
    alt.Chart(
        pl.concat(
            [
                pl.DataFrame(
                    {
                        "x": [0.1, 0.2, 0.3, 0.4, 0.5],
                        "y": [0.1, 0.2, 0.3, 0.4, 0.5],
                        "id": ["1", "1", "1", "1", "1"],
                    }
                ),
                pl.DataFrame(
                    {
                        "x": [1.1, 1.2, 1.3, 1.4, 1.5],
                        "y": [1.1, 1.2, 1.3, 1.4, 1.5],
                        "id": ["2", "2", "2", "2", "2"],
                    }
                ),
            ]
        )
    )
    .mark_line()
    .encode(column="id")
)

(
    base.encode(
        x=alt.X("x").scale(zero=False),
        y=alt.Y("y").scale(zero=False),
    )
    .resolve_scale(x="independent", y="independent")
    .show()
)

However, this obviously distorts the tracks. I should have used latitude and longitude instead of x and y. So I tried this:

(
    base.encode(
        latitude=alt.Latitude("x"),
        longitude=alt.Longitude("y"),
    )
    .resolve_scale(latitude="independent", longitude="independent")
    .show()
)

This errors because resolve_scale does not accept latitude or longitude as inputs.

Is there a fundamental issue as for why resolve_scale can't work with latitude / longitude? Could this be added?

I first wanted to open this under altair but I guess this would have moved here eventually? Sorry for only providing the python code.

Thanks a lot for the great work you're doing!

@kjgoodrick
Copy link

kjgoodrick commented Jan 6, 2025

I believe this is related to the issue #3729 which prevents faceting on geoshape plots.

As a workaround you can manually concatenate each plot. When doing this you will also encounter #9321 which causes concatenated geoshape plots to have shared projections, so it is also necessary to slightly change the projection on each loop.

import altair as alt
import polars as pl

base_df = pl.concat(
    [
        pl.DataFrame(
            {
                "x": [0.1, 0.2, 0.3, 0.4, 0.5],
                "y": [0.1, 0.2, 0.3, 0.4, 0.5],
                "id": ["1", "1", "1", "1", "1"],
            }
        ),
        pl.DataFrame(
            {
                "x": [1.1, 1.2, 1.3, 1.4, 1.5],
                "y": [1.1, 1.2, 1.3, 1.4, 1.5],
                "id": ["2", "2", "2", "2", "2"],
            }
        ),
    ]
)

base = (
    alt.Chart(base_df)
    .mark_line()
    .encode(
        latitude=alt.Latitude("x"),
        longitude=alt.Longitude("y"),
    )
)

alt.concat(
    *(
        base.transform_filter(alt.datum.id == run_id).project(
            precision=0.707 + (count / 1e6)
        )
        for count, run_id in enumerate(base_df["id"].unique())
    )
)

@thomascamminady
Copy link
Author

Thanks for the workaround!
Happy to see this issue closed if it is ultimately going to be resolved by #3729 .

@domoritz
Copy link
Member

domoritz commented Jan 8, 2025

If there is an easier solution that doesn't involve trying to fully fix facte for geoshape, I am happy to review a pull request. Happy to reopen this if this is something you can help with.

@domoritz domoritz closed this as completed Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants