Visualizing Sales on Geographical Map Using Python¶
# Underlying Data
In this blog we will use python to plot and visualize total Sales on the geographical Map based upon the state. That is total sales for each state is plotted and visualized in Geographical Map.
For this use case we will use SampleSuperStore Data and Geo Spatial Data for US States. The Geo Spatial Data can be downloaded from the below link :
https://github.com/python-visualization/folium/tree/master/examples/data
We will use python library “folium” to plot the desired details on Geographical map using Geo Spatial Data.
To install a new python library please refer to the below link :
https://instrovate.com/2019/06/04/image-processing-1-getting-started-with-opencv-for-python/
The first step is to declare the paths with filename of the above two requried files mentioned above.
We will use Pandas (A python library) to read the Sample Super Store Data stored in Excel file
Code is as below
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 28 13:32:00 2019
@author: Instrovate
"""
import folium
import pandas as pd
import os
spaitial_data_file_name = 'us-states.json'
sample_super_store = 'SampleSuper.xls'
SampleSuperdata = pd.read_excel(sample_super_store)
# Selecting Only Useful Columns
Now we will select only State and Sales as we will be visualizing total sales for each state
usefull_columns = SampleSuperdata[["State","Sales"]]
usefull_columns
# Sales Aggregation
Since, the above data depict the sales for ech each order occured. So, for each state it will be having multiple entries as there could be multiple order made for each state over perioid of time.
So, the next step is to aggregate the sales value based on state. We will take in consideration total sales value for each state. This can be done by Pandas Data frame Data manipulation function “groupby” as defined below:
state_data = usefull_columns.groupby(["State"],as_index=False).sum()
state_data
# Visualizing Sales on Map
We will use python library “folium” to plot the desired details on Geographical map using Geo Spatial Data.
To install a new python library please refer to the below link :
https://instrovate.com/2019/06/04/image-processing-1-getting-started-with-opencv-for-python/
Below is the python code with output visualization on Map
state_sales_map = folium.Map(location=[37, -102], zoom_start=5)
# Set color for chloropleth:
state_sales_map.choropleth(
geo_data=spaitial_data_file_name,
name='choropleth',
data=state_data,
columns=['State', 'Sales'],
key_on='properties.name',
fill_color='PuBu',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Sales'
)
folium.LayerControl().add_to(state_sales_map)
# Save the Map to a HTML File .
state_sales_map.save('SampleSuperStoreSalesOnMap.html')
state_sales_map
# If your browser is working fine , you should be able to see the Geographical Map below . If there is some browser
issue and you are not able to see the Map , The other way to view the Map is save the file and open it through Chrome Browser .