Visualizing Crime against women in India on a Map using Geopandas
By: Yaser Sakkaf
Tags: Geopandas, Data Science, Data Visualization, Python

Visualizing Crime against women in India on a Map using Geopandas

This article shows us a simple way to plot either state-wise or district-wise statistical data (like the one used here i.e. crime against women in India) on the Indian Map(or you can choose any country) using GeoPandas: A Python Library.

Image Courtesy: Reyna Zamora , [Pinterest Post]. Retrieved Feb 18, 2020, from https://in.pinterest.com/pin/626563366894685267/

Introduction:

Hello everyone, this article demonstrates how to plot the data for crimes against women on a choropleth map of India with respect to each state.

According to Google Dictionary: “A choropleth map is a map which uses differences in shading, colouring, or the placing of symbols within predefined areas to indicate the average values of a particular quantity in those areas.”

Geopandas:

Geopandas is a library that can be used to create choropleth maps with not too many lines of code! It can read files as dataframes which are usually called Geodataframes. Geodataframes are a lot like Pandas dataframes, so the two usually play along nicely. More about Geopandas https://geopandas.org/

Shape Files:

A shapefile is a simple, nontopological format for storing the geometric location and attribute information of geographic features. Geographic features in a shapefile can be represented by points, lines, or polygons (areas).

The shape files used in this article to plot the India map with state boundaries can be downloaded from this link while that with the district boundaries can be downloaded from this link.

Installation using Pip:

You can installl the Geopandas library using pip as shown below:

pip install geopandas

pip install descartes

The library descartes is a dependency for geopandas which has to be installed explicitly, so don’t forget to install it too.

Implementation:

We explain the entire implementation in Python in 5 simple steps

Suppose we have an excel file named state_wise_crimes.xlsx containing all the India State names along with the Total Crime against women as shown below:

+-------------------+----------------------------+

| state_name | Total Crimes against Women |

+-------------------+----------------------------+

| Andhra Pradesh | 15931.0 |

| Arunachal Pradesh | 384.0 |

| Assam | 23258.0 |

| Bihar | 13891.0 |

| Chhattisgarh | 5720.0 |

| ..... | ..... |

+-------------------+----------------------------+

We follow the following steps to create the chloropeth map

Step 1: Read the excel file into a pandas dataframe:

df = pd.read_excel('state_wise_crimes.xlsx')

Step 2: Read the Indian map shapefile with district boundaries in a Geodataframe:

fp = "Igismap/Indian_States.shp"

map_df = gpd.read_file(fp)

Step3: Join both dataframes by state names:

merged = map_df.set_index('st_nm').join(data_for_map.set_index('state_name'))

merged.head()

Step4: Create figure and axes for Matplotlib and set the title

fig, ax = plt.subplots(1, figsize=(10, 6))

ax.axis('off')

ax.set_title('State Wise Crime against women in India in 2015', fontdict={'fontsize': '25', 'fontweight' : '3'})

Step6: Finally, plot the chloropeth map

merged.plot(column='Total Crimes against Women', cmap='YlOrRd', linewidth=0.8, ax=ax, edgecolor='0.8', legend=True)

Output:

The output should look like this

You can also save your output as an image using the code snippet shown below:

fig.savefig("State_wise.png", dpi=100)

Similarly we can plot the district wise chloropeth map using its shape file mentioned in the Shape Files section. You can download the excel file containing the district wise crimes against women from this link.

The output for which looks like:

We can do the same for any single state too, just like I did for Maharashtra.

For this purpose we can use the same shape files that were used for district wise visualization by adding a single line:

map_df = map_df[map_df['NAME_1']=='Maharashtra']

The output looks like:

Github link:

You can get the entire code here:

Summary:

In this article we saw how to use GeoPandas library to plot chloropeth maps of India to describe the state wise and district wise crimes against women in 2015.

With appropriate data sets and shape files, we can do the same thing for any country or state in it.

References: