has2k1 / has2k1/plotnine

Support for Basemap

Open
#918 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature
Dominant language
Python
Stars
4.8k
Forks
254
Avg merge
10h 35m
Merged PRs (30d)
9

Description

@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 https://github.com/has2k1/plotnine/discussions/850#discussioncomment-12436571

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposed custom geom_basemap implementation and how plotnine.ggplot handles its draw phase. Compare the contextily basemap example with the Grammar of Graphics concepts, then determine the appropriate API and category for basemap support. Done means an agreed design with implementation and coverage for the supported usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data-visualization
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.