Support for Basemap

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

Nobody has claimed this yet.

Assessment

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

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.

Description

Feature

@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

Dominant language
Python
Stars
4.8k
Forks
269
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

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.

More from has2k1/plotnine

All issues in has2k1/plotnine

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.