Open In App

How to re-size Choropleth maps – Python

Last Updated : 24 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how to create resizable maps using different libraries in Python.

Choropleth maps are a type of thematic map that displays divided regions or territories shaded or patterned in relation to a specific data variable. In Python, choropleth maps can be created using various libraries, such as matplotlib, plotly, geopandas.

Choropleth maps are a powerful tool to display data over a geographical region. In Python, the most commonly used libraries for creating choropleth maps are geopandas and plotly. To resize choropleth maps in Python, we can use the layout() function in plotly or matplotlib to adjust the figure size. Choropleth maps are commonly used in various fields, such as geography, economics, and public health, to visualize and analyze spatial patterns and trends in data. Here we are discussing three methods through which we can create resizable choropleth maps.

Prerequisites

Install the required libraries using the pip command given below.

pip install geopandas
pip install matplotlib
pip install plotly

Choropleth map using the GeoPandas library

In this example, we first load the required libraries which are geopandas and matplotlib. Load the naturalearth_lowres dataset and then, we create a choropleth map of the countries by plotting the gdp_md_est column, which represents the estimated gross domestic product. We use the OrRd color map and include a legend. Next, we resize the plot by getting the current figure object with plt.gcf(), setting its size using the set_size_inches() method, and displaying the plot using plt.show(). In this case, we set the figure size to 12 inches wide and 6 inches tall. we can adjust the size to fit your specific needs. we can resize that map using the components which are showing in the output.

Python




# import library for create choropleth map
import geopandas as gpd
import matplotlib.pyplot as plt
 
# Load the data and create a choropleth map
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.plot(column='gdp_md_est',
           cmap='OrRd', legend=True)
 
# Resize the plot
fig = plt.gcf()
fig.set_size_inches(12, 6)
 
# show map
plt.show()


Output :

Choropleth map using the GeoPandas library

 

Choropleth map using the Plotly library 

In this, we import the Plotly library. This code uses the plotly.graph_objects module to create a Choropleth map of the United States, displaying data for three states (California, Texas, and New York) with corresponding values (1, 2, and 3) represented by color. we can resize that map using the components which are showing in the output.

Python




# import ploty libraray
import plotly.graph_objects as go
 
# create figure using fig method
fig = go.Figure(data=go.Choropleth(
    locations=['CA', 'TX', 'NY'],
    z=[1, 2, 3],
    locationmode='USA-states',
    colorscale='Viridis',
))
 
# Resize the code using
# update layout figure method
fig.update_layout(
    title_text='GeeksforGeeks',
    geo=dict(scope='usa', showlakes=True,
             lakecolor='rgb(85,173,240)'),
    width=500,
    height=500,
)
 
#show map
fig.show()


Output : 

Choropleth map using the Plotly library

 

Choropleth map using the Plotly Express Library 

The below code is using the Plotly Express library to create a choropleth map that visualizes the voting results of an election we can resize that map using the components which are showing in the output.

Python




# import ploty.express libraray
import plotly.express as px
 
# Load data
df = px.data.election()
 
# Create choropleth map
fig = px.choropleth(df,
                    locations="district",
                    color="Bergeron",
                    hover_name="district",
                    range_color=[0, 50],
                    color_continuous_scale="agsunset")
 
# Resize map by height and width
fig.update_layout(height=400,
                  width=600)
 
# Show map
fig.show()


Output : 

Choropleth map using the Plotly Express Library

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads