plotly / plotly/plotly.py

Choropleth maps only render the first feature with a matching featureidkey, not all matching features

Ouverte
#4,660 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

bug P3
Langage dominant
Python
Étoiles
18.8k
Forks
2.8k
Merge moyen
16 h 26 min
PR mergées (30 j)
21

Description

When px.choropleth() is called with a GeoJSON feature collection containing multiple features matching a single featureidkey value, only the first feature is rendered. I believe all features should be so rendered.

The following code generates two images: the first renders all four features, while the second renders only three. The first uses a featureidkey unique to all features, the second uses a featureidkey that is not unique -- only the first feature to match is rendered. :-(

#!/usr/bin/env python3

import pandas as pd
import plotly.express as px

# A simple GeoJSON with four features.  
# Each feature has a district.  
# One district has two features.
geojson = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "name": "Alpha",
                "district": "One",
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [[[0, 0], [0, 5], [5, 5], [5, 0], [0, 0]]],
            },
        },
        {
            "type": "Feature",
            "properties": {
                "name": "Bravo",
                "district": "One",
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [[[5, 0], [5, 5], [10, 5], [10, 0], [5, 0]]],
            },
        },
        {
            "type": "Feature",
            "properties": {
                "name": "Charlie",
                "district": "Two",
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [[[0, 5], [0, 10], [5, 10], [5, 5], [0, 5]]],
            },
        },
        {
            "type": "Feature",
            "properties": {
                "name": "Delta",
                "district": "Three",
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [[[5, 5], [5, 10], [10, 10], [10, 5], [5, 5]]],
            },
        },
    ],
}

districts = {
    f["properties"]["name"]: f["properties"]["district"] for f in geojson["features"]
}

# A simple dataframe relating names and districts from the above GeoJSON data.
# i.e., [{'name': 'Alpha', 'district': 'One'}, ...]
data = [f['properties'] for f in geojson['features']]

df = pd.DataFrame(data)

# This code generates two maps from the data:
# * The name map shows all four squares in the same color (count = 1)
# * The district map shows _three_ squares, one with one color (count = 2) and two with another color (count = 1)

# What I expected:
# * The district map should show all four squares, two with light color and two with dark
for key in ["name", "district"]:

    region_counts = df.groupby([key], observed=False).size().reset_index(name="count")

    fig = px.choropleth(
        region_counts,
        geojson=geojson,
        fitbounds='geojson',
        locations=key,
        color="count",
        featureidkey=f"properties.{key}",
    )

    fig.write_image(f"mwe-{key}.png")

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Reproduisez les deux appels à px.choropleth() du script fourni, en comparant le nom unique et les valeurs de district répétées via locations et featureidkey. Suivez le point d’entrée de choropleth et la mise en correspondance des features GeoJSON, puis vérifiez que l’opération est terminée en rendant les quatre polygones sur la carte des districts tout en conservant le résultat du nom unique.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
data-visualization
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
28/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.