plotlylegend-properties

Placing legend below map plot using Plotly


I am trying to make a world population map using Plotly, for the first time. The plot is fine except that the legend is too far away from the plot.

I'd like to ask for suggestions on 1) how to reduce the gap between the plot and the legend, and 2) how to place the legend below the plot.

Here are the steps I am following:

import pandas as pd
import plotly.express as px

url = 'https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population'
dfs = pd.read_html(url)
df = df.iloc[:, 1:3]

df = df[df['Location'] != 'World']

fig = px.choropleth(df, 
                     locations='Location', 
                     locationmode='country names', 
                     color='Population',
                     color_continuous_scale='Viridis_r', 
                     range_color=(df['Population'].min(), df['Population'].max()),
                     labels={'Population': 'Population'},
                     scope='africa')

fig.update_geos(showcountries=True, countrycolor="darkgrey")
fig.update_layout(
    title='Population by Country', 
    title_x=0.5, 
    geo=dict(showframe=False, showcoastlines=False, projection_type='equirectangular'),
    coloraxis_colorbar=dict(
        title='Population', 
        x=0, y=-0.1, 
        xanchor='left', yanchor='bottom',
        len=0.6
    )
)


fig.show()

Thanks in advance!

I tried the methods from the existing answers which didn't work for my case.


Solution

  • To make the color bar horizontal, set the direction of the color bar to horizontal and set the desired x,y positions. xy settings range from 0 to 1 for each if the graph area is defined as 'paper'. See here for more information.

    import pandas as pd
    import plotly.express as px
    import requests
    
    url = 'https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population'
    
    r = requests.get(url)
    df_list = pd.read_html(r.text)
    df = df_list[0]
    df = df.iloc[1:, 1:3]
    
    fig = px.choropleth(df, 
                         locations='Location', 
                         locationmode='country names', 
                         color='Population',
                         color_continuous_scale='Viridis_r', 
                         range_color=(df['Population'].min(), df['Population'].max()),
                         labels={'Population': 'Population'},
                         scope='africa')
    
    fig.update_geos(showcountries=True, countrycolor="darkgrey")
    fig.update_layout(
        title='Population by Country', 
        title_x=0.5, 
        geo=dict(showframe=False, showcoastlines=False, projection_type='equirectangular'),
        coloraxis_colorbar=dict(
            title='Population', 
            x=0, y=-0.1, 
            xanchor='left', yanchor='bottom',
            len=0.6
        )
    )
    fig.update_coloraxes(colorbar={'orientation':'h', 'thickness':20, 'y': -0.3, 'x':0.4, 'xanchor': 'center'})
    fig.update_layout(width=550)
    fig.show()
    
    

    enter image description here