whatserface
whatserface

Reputation: 69

How to remove Antarctica from plotly world map?

I'm using plotly python library in order to make a choropleth. The data of choropleth (choc_df) has been handled by pandas. The choropleth is drawn by this code:

import plotly.graph_objects as go

go.Figure(data=go.Choropleth(
    locations=choc_df['ISO'],
    text=choc_df['company_location'],
    marker_line_color='black',
    marker_line_width=0.5,
    autocolorscale=False,
    colorscale='brbg_r',
    z=choc_df['rating'],
    customdata=choc_df['number'],
    hovertemplate="<br>".join([
        "<b>%{text}</b>",
        "Number of brands: %{customdata}",
        "Rating: %{z}",
    ]) + '<extra></extra>',
), layout=dict(title='Rating',
        geo=dict(showcountries=True, countrycolor='#444444', showlakes=False)
))

I'm happy with the result: resulting_choropleth The only thing that bothers me is the amount of space that Antartica takes for no reason :D. Is there a way to remove it from map?

Upvotes: 2

Views: 765

Answers (2)

amance
amance

Reputation: 1770

I got it! Color Antarctica and it's border white (or whatever color your background is).

#using Plotly sample data
df = px.data.gapminder().query("year==2007")

fig = px.choropleth(df, locations="iso_alpha",
                color="lifeExp", # lifeExp is a column of gapminder
                hover_name="country", # column to add to hover information
                color_continuous_scale=px.colors.sequential.Plasma)

Original

Original

"Hidden" Antarctica

fig.add_trace(go.Choropleth(locations=['ATA'],
            z=[0],
            colorscale=[[0,'white'], [1,'white']],
            marker_line_color='white',
            showlegend=False,
            showscale=False)
      )

"Hidden" Antarctica

Optional Zoom

fig.update_geos(lataxis_range=[-59, 90])

Optional Zoom

Upvotes: 2

AKX
AKX

Reputation: 168996

You can apparently set lataxis_range=[-50,20] and lonaxis_range=[0, 200] in the geo dict, so possibly setting lataxis_range=[-90, 80] might crop Antarctica out.

You can also try automatic bounds with fitbounds="locations" to see if that manages to crop out Antarctica too, since it has no data.

Upvotes: 2

Related Questions