top of page

How to make Geoplots in python?

Data visualization plays a crucial role in understanding and interpreting complex datasets. Geospatial data, in particular, offers unique insights when visualized effectively. In this tutorial, we will explore the powerful Python library called Geoplot, which allows us to create visually compelling maps and geospatial visualizations. We will walk through each step of the code and explain the concepts behind it, enabling beginners to grasp the fundamentals of geospatial data visualization. Let's get started!

Step 1: Importing Relevant Packages To begin, we need to import the necessary packages to work with geospatial data. In Python, we commonly use pandas, Path from pathlib, geopandas, and matplotlib.pyplot. These packages provide essential functionality for data manipulation, file handling, geospatial analysis, and visualization.

# %%

# * import relevant packages

import pandas as pd

from pathlib import Path

import geopandas as gpd

import matplotlib.pyplot as plt

Step 2: Specifying Parameters and Paths In this step, we set the parameters for our analysis. We define the specific year and countries of interest. Additionally, we specify the directory path where our data is located.

# * Specify parameters

Years = [2020]

countries = ["Russia", "China", "United States of America", "Canada", "Brazil", "Austrailia", "India", "Argentina"]

# * Specify paths

directory_path = Path("..")

Step 3: Loading and Preparing the Data Now, we proceed to load the necessary data for our analysis. We read the land use data and the geospatial data using pandas and geopandas, respectively. We also preprocess the data by filtering and selecting the relevant information.

#%%

#Loading the necessary data

land_use = pd.read_csv("FAOSTAT_data_en_1-22-2023.csv")

geo_world = gpd.read_file("custom.geo.json")

#print(land_use.head(10))

land_use = land_use.rename(columns = {"Area" : "country"})

land_use = land_use[land_use["Year"].isin(Years)]

#land_use = land_use[land_use["country"].isin(countries) ]

land_use = land_use[land_use["Item"] == "Arable land"]

land_use = land_use[["country","Year","Item","Value"]]

#ev_europe = ev_europe[ev_europe["Powertrain"] == "BEV"]

print(land_use.head(10))

#geo_world =geo_world[geo_world["name"].isin(countries)]

geo_world =geo_world.rename(columns = {"name":"country"})

geo_world = geo_world.sort_values("country",ascending =True)

print(geo_world.head(10))

Step 4: Combining Dataframes and Exporting Data In this step, we merge the land use data with the geospatial data based on the common "country" column. This combined dataframe will be used for further analysis and visualization. We also export the merged dataframe as a CSV file for future reference.

# %%

#Combining dataframes

combined_geo_df = pd.merge(geo_world,land_use, on="country")

print(combined_geo_df.head(10))

combined_geo_df.to_csv(directory_path/"land_use_combined.csv",index = False)

Step 5: Visualizing the Land Use Data Finally, we arrive at the exciting part – visualizing the land use data on a map using Geoplot and Matplotlib. We create a plot object, set the colormap and color range, and plot the data. This generates a beautiful map with color-coded regions representing the land use values.

#%%

#Min and max values

total_landuse_min = int(combined_geo_df["Value"].min())

print(total_landuse_min)

total_landuse_max = int(combined_geo_df["Value"].max())

print(total_landuse_max)

# %%

# * geoPlot for carbon intensity of electricity grid

fig, ax = plt.subplots(1, figsize=(8, 7))

combined_geo_df.plot(

    column="Value", cmap="BuGn", linewidth=0.8, ax=ax,

    edgecolor='black'

                  )

sm = plt.cm.ScalarMappable(cmap="BuGn", norm=plt.Normalize(

    vmin=total_landuse_min, vmax=total_landuse_max)

                           )

sm._A = []

cbar = fig.colorbar(sm)

cbar.set_label('Value')

 

ax.axis("off")

ax.set_title("Landuse",

             fontdict={"fontsize": "16", "fontweight": "2"}

             )

By executing the above code, we generate a geospatial plot that visualizes the land use data. Each region is colored according to its land use value, creating an intuitive representation of land use patterns across different countries.

Conclusion: In this tutorial, we explored the powerful Geoplot library in Python for geospatial data visualization. We started by importing the necessary packages and specifying the parameters and paths. Then, we loaded and prepared the data, merging the geospatial and land use datasets. Finally, we used Geoplot and Matplotlib to create an interactive map visualizing the land use data.

Geospatial visualization allows us to gain valuable insights into various phenomena, such as land use patterns, population density, and environmental factors. With Geoplot, you can unlock the potential of your geospatial data and present it in an engaging and informative manner.

Experiment with different datasets, explore advanced visualization techniques, and unleash the power of geospatial data visualization with Geoplot. Happy mapping!

Remember to share your feedback and suggestions for improvement. Stay curious and keep exploring the fascinating world of data science!

References:

That concludes our tutorial on geospatial data visualization with Geoplot in Python. We hope you found this guide helpful in understanding the process of visualizing geospatial data. Feel free to adapt and modify the code to suit your specific needs and explore further possibilities in geospatial visualization. Happy coding!

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page