简体   繁体   中英

Plotly Dash: go.Choropleth subunitwidth not working

Can somebody give me a hint of why this part of my go.Choropleth code is not working?

I'm trying to make my country's borders a little more thicker and black, but it's not working and I don't know what else I could be missing on this layout specs... Here is my map code section and below it you can check a zoomed part of the map generated. Notice the country's borders are still thin and grey, why it does not change?

map_fig_layout = {'coloraxis_colorbar': {'title': 'População (%)',
                                                  'thickness': 20,                  
                                                'ticklabelposition':'outside bottom'},
                         'margin': {'r':0, 'l':0, 't':0, 'b':0},
                         'template': 'plotly_dark',
                         'geo':{'projection': go.layout.geo.Projection(type ='natural earth'),
                                'landcolor': '#262626',
                                'showcountries':True,
                                'showsubunits':True,
                                'subunitcolor': 'black',
                                'subunitwidth': 4,
                                'resolution':110,
                                'visible':True,
                                'countrywidth': 4,
                                'countrycolor' : 'black'},                       
                         'uirevision':'not_tracked_key'}

       map_graph = go.Figure({'data':[ go.Choropleth(locations = dff['Code'],
                                                     z = dff['população_%'], # a column of dff
                                                     hovertext = dff['Entity'],
                                                     zmin = 0,
                                                     zmax = 100,
                                                     colorscale = make_colorscale( ['#F53347','#E6C730','#2FF5A8'] ),
                                                     geo = 'geo') ],
                              'layout': map_fig_layout})
    

地图的放大部分

  • using your code (make sure you format it to be PEP8 compliant for future questions)
  • sourced some data from kaggle to make this a re-producible example
  • examples of subunits are counties / regions in a country
import kaggle.cli
import sys, requests
import pandas as pd
from pathlib import Path
from zipfile import ZipFile
import urllib
import plotly.graph_objects as go
from plotly.colors import make_colorscale

# fmt: off
# download data set
url = "https://www.kaggle.com/mohaiminul101/population-growth-annual"
sys.argv = [sys.argv[0]] + f"datasets download {urllib.parse.urlparse(url).path[1:]}".split(" ")
kaggle.cli.main()
zfile = ZipFile(f'{urllib.parse.urlparse(url).path.split("/")[-1]}.zip')
dfs = {f.filename: pd.read_csv(zfile.open(f)) for f in zfile.infolist()}
# fmt: on

dff = dfs["world_population_growth.csv"].rename(
    columns={"Country Code": "Code", "2019": "população_%", "Country Name": "Entity"}
)
dff["população_%"] = dff["população_%"] * 100
map_fig_layout = {
    "coloraxis_colorbar": {
        "title": "População (%)",
        "thickness": 20,
        "ticklabelposition": "outside bottom",
    },
    "margin": {"r": 0, "l": 0, "t": 0, "b": 0},
    "template": "plotly_dark",
    "geo": {
        "projection": go.layout.geo.Projection(type="natural earth"),
        "resolution": 110,
        "visible": True,
    },
    "uirevision": "not_tracked_key",
}

map_graph = go.Figure(
    {
        "data": [
            go.Choropleth(
                locations=dff["Code"],
                z=dff["população_%"],  # a column of dff
                hovertext=dff["Entity"],
                zmin=0,
                zmax=100,
                marker={"line":{"width":4, "color":"black"}},
                colorscale=make_colorscale(["#F53347", "#E6C730", "#2FF5A8"]),
            )
        ],
        "layout": map_fig_layout,
    }
)

map_graph

在此处输入图片说明

Try adding this line to your code:

fig.update_traces(marker_line_width=2.0, selector=dict(type='choropleth'))

or in your case:

map_graph.update_traces(marker_line_width=2.0, selector=dict(type='choropleth'))

You can additionally control the opacity, if so desired:

Eg,

from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)

import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
                   dtype={"fips": str})

import plotly.express as px

fig = px.choropleth(df, geojson=counties, locations='fips', color='unemp',
                           color_continuous_scale="Viridis",
                           range_color=(0, 12),
                           scope="usa",
                           labels={'unemp':'unemployment rate'}
                          )
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.update_traces(marker_line_width=3.0, marker_opacity=0.6, selector=dict(type='choropleth'))
fig.show()

增加地图边界

Docs References

https://plotly.com/python/reference/choropleth/#choropleth-marker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM