8000 Support for Basemap · Issue #918 · has2k1/plotnine · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support for Basemap #918

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

Open
has2k1 opened this issue Mar 10, 2025 · 3 comments
Open

Support for Basemap #918

has2k1 opened this issue Mar 10, 2025 · 3 comments
Labels

Comments

@has2k1
Copy link
Owner
has2k1 commented Mar 10, 2025

@nimisha-18, I was able to add a basemap by implementing a custom geom subtype that adds the basemap during plotnine.ggplot's draw phase:

from __future__ import annotations

import typing

import pandas as pd
from plotnine.geoms.geom import geom
import contextily as ctx

if typing.TYPE_CHECKING:
    from typing import Any

    import pandas as pd
    from matplotlib.axes import Axes

    from plotnine.coords.coord import coord
    from plotnine.iapi import panel_view


class geom_basemap(geom):
    DEFAULT_PARAMS = {
        "stat": "identity",
        "position": "identity",
        "na_rm": False,
        "crs": "EPSG:3857",
        "source": ctx.providers.OpenStreetMap.Mapnik,
    }

    def draw_panel(
        self,
        data: pd.DataFrame,
        panel_params: panel_view,
        coord: coord,
        ax: Axes,
        **params: Any,
    ):
        crs = params["crs"]
        source = params["source"]
        ctx.add_basemap(ax, crs=crs, source=source)

To use it in your example, you would just add it like any other layer:

plot3 = plot2 + geom_basemap(source=ctx.providers.OpenStreetMap.Mapnik)
plot3.show()

Aside, the geom_basemap concept as a geom, at least to me, doesn't fit well with the ideas outlines in The Grammar of Graphics. Im not sure what category a basemap falls within so calling this a geom is likely conceptually incorrect. Just something to think about.

Originally posted by @aaraney in #850 (reply in thread)

@has2k1
Copy link
Owner Author
has2k1 commented Mar 10, 2025

@aaraney You are right about geom_basemap. A better solution may be adding parameters to geom_map that turnon basemap. Something like.

geom_map(
    basemap={
        "source": ctx.providers.OpenStreetMap.Mapnik,
         "crs": "EPSG:3857", # if None, the default is a value from the geodataframe
        ...
    }
)

I hope to work on the mapping projections (supported by cartopy) later this year and I will be in a better position to think about how to add basemap support.

@has2k1 has2k1 added the Feature label Mar 10, 2025
@aaraney
Copy link
aaraney commented Apr 16, 2025

@has2k1, the previous comment by @slapes7 looks like its part of an ongoing malware attack: https://any.run/report/a96b7342c5dc45bcb79b0cb7839d9c89461c4faec5ee3278fc56729ebd872262/926ed16a-6155-4cb1-a696-53125217e4ff. Can you remove it / report it?

@has2k1
Copy link
Owner Author
has2k1 commented Apr 16, 2025

@aaraney I think github has detected it and others and removed them. Without your note, I was confused about the notifications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants
@has2k1 @aaraney and others
0