Creating Map using Python to Visualize Your Dataset

rindangchi
5 min readJun 26, 2023
Sales Dashboard : https://tinyurl.com/29jcu5p9

Map is one graphical representation that is sometimes more interesting than usual graph such as barchart, pie chart, or line chart. Maps can attract more audiences to explore your data visualization or presentation because they make audiences grasp information more easily and quicly. There are many times when visualization using maps is highly crucial. For example, maps can be used when you want to visualize demographic characteristics in a specific region, mapping population, mapping spread of disease, or representing transportation route. There are also many types of maps can be chosen to visualize your data.

Several times ago I work on an EDA project and i wonder if it is possible to create maps using python like the one that I have created in Tableau project before. In this post I will share my knowledge on how to creating map in python, it is actually a basic map but I will continously update it when I gain more advance knowledge.

Creating map in python is not as simple as the way we create it in Tableau by dragging and dropping, but it is not too complex also. Actually there are many libraries can be used to create maps using python. Python also offers multitude techniques for generating maps, but in this post I will just explain how creating maps using GeoPandas and Chloropete.

Creating Maps Using Geopandas

To create maps with geopandas, we need to install and import some libraries.

pip install Fiona
pip install GDAL
pip install RTree
pip install Shapely
pip install GeoPandas
import pandas as pd
import matplotlib.pyplot as plt
import geopandas
from shapely.geometry import Point
import missingno as msn

#to load file from google drive
from google.colab import drive
drive.mount('/content/drive')

Next we need to import json data containing the name of location and the geometry.

#load json file from google drive
state = geopandas.read_file("/content/drive/My Drive/gz_2010_us_040_00_500k.json")
state

We also need to import csv file containing the performance or KPI data that we want to visualize. Here I used the sales superstore data containing sales, quantity, discount, and profit value.

file_path = '/content/drive/My Drive/SampleSuperstore.csv' 
data = pd.read_csv(file_path)
#print the data
data.head()

I sum the data by state so that it will give the total value for each state.

df_grp_state = data.groupby(['State']).sum()
df_grp_state

Next, I will merge the two datframes into one dataframe named merged. So, we will get the geometry data and the values in one dataframe. I will also drop null value from the dataframe.

merged = pd.merge(left = state,right = df_grp_state, left_on = 'NAME', right_on = 'State', how = 'left')
merged= merged.dropna()
merged

Next, we will just need to generate the map. Here I create map for the sales value.

ax = merged.boundary.plot(edgecolor = 'black', linewidth = 0.2, figsize=(10,5)) #creating the border
merged.plot(ax= ax, column = 'Sales', legend = True, cmap='GnBu') # creating the map and fetch the sales column as its value, set the color and display the legend
ax.get_xaxis().set_visible(False) # hide the x axis value
ax.get_yaxis().set_visible(False) # hide the y axis value

for edge in ['right', 'bottom', 'top', 'left']:
ax.spines[edge].set_visible(False)
ax.set_title('Sales per State', size = 12, weight = 'bold')

plt.show()

And here is the map. The map can represent the frequency of sales value in each region, but unfortunately it can not give clear information about the name of the state. We see that the highest sales value is in the left corner buut we can not see what state it is.

We can create more informative map using another way, we can use chloropete.

Creating Maps Using Chloropete

To create map using chloropete we will use another json file that contains the state code, such as MD for Maryland, MI for Michigan and soon.

# import new json file 
json_2 = geopandas.read_file("/content/drive/My Drive/us-states.json")
json_2

Like before I will also merge the json file with the csv file.

#merge json with the csv file 
merged_2 = pd.merge(left = json_2,right = df_grp_state, left_on = 'name', right_on = 'State', how = 'left')
merged_2

We need to import other libraries to create map using chloropete.

pip install plotly
import plotly.express as px
import plotly.graph_objects as go
fig = go.Figure(data=go.Choropleth(
locations=merged_2['id'],
z = merged_2['Sales'],
locationmode = 'USA-states',
colorscale = 'GnBu',
colorbar_title = "USD",
text=merged_2[['id','name','Sales']],
))

fig.update_layout(
title_text = 'Sales By State',
geo_scope='usa', # limit map scope to USA
)

fig.show()

And here the result. It looked same, but when we hover to the map we wil show information regarding the state or region and the sales value. In the legend we can also see the currency used.

In conclusion, it is very possible to create maps using python library and it is quite flexible and easy. There are many kind of maps can be used for our visualization projects. Creating maps using chloropote is more informative because it gives us label information when we hover on the maps.

Next I will explore more about the maps creation using python. So stay tune and don’t forget to follow.

You can visit my github to know other posts about data analysis and visualization: https://github.com/rindangchi

Finally, let’s connect each other and keep learning together !!

Reference:

--

--