pythonplotlyplotly-dashplotly-pythonchoropleth

How to modify country border thickness, color, and style in Plotly Choropleth map?


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 thicker and more black, but it's not working and I don't know what else I could be missing in these layout specs. Here is my map code section and below it you can see a zoomed-in part of the generated map. Notice the country's borders are still thin and grey - why does it 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})
    

Zoomed part of the map


Solution

  • 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:

    E.g.,

    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()
    

    increased map borders

    Docs References

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