Map of India with Python Basemap
In this tutorial, we will use the Basemap library of Python to plot map of India as well as mark the top ten most populated cities on that map. Here, we will use the dataset provided by Kaggle to plot the top 10 most populated cities.
Here, we will use pandas, bumpy, matplotlib, matplotlib.pyplot, and Basemap library of Python.
# importing packages import pandas as pd import numpy as np from numpy import array import matplotlib as mpl # for plots import matplotlib.pyplot as plt from matplotlib import cm from mpl_toolkits.basemap import Basemap %matplotlib inline
Among these Basemap might show error in importing. To tackle that use the following code to import Basemap.
!sudo apt-get install libgeos-3.5.0 !sudo apt-get install libgeos-dev !sudo pip install https://github.com/matplotlib/basemap/archive/master.zip
The dataset used could be downloaded from https://www.codespeedy.com/wp-content/uploads/2020/06/datasets_557_1096_cities_r2.csv
Use the following code to import the dataset. The text in quotes gives the path to where you have downloaded the above dataset.
cities = pd.read_csv ("/content/drive/My Drive/codespeedy/datasets_557_1096_cities_r2.csv")
cities.head() will give the first five entries of the dataset to get an idea of what the dataset is about.
Getting what to plot:
Now we will plot a bar graph of states vs the number of cities we have taken for analysis.
fig = plt.figure(figsize=(20,20)) states = cities.groupby('state_name')['name_of_city'].count().sort_values(ascending=True) states.plot(kind="barh", fontsize = 20) plt.grid(b=True, which='both', color='Black',linestyle='-') plt.xlabel('No of cities taken for analysis', fontsize = 20) plt.show ()
We have used fig variable to store our design. It has a size of 20×20. Then we have created a variable “states” to store the value of states in terms of the number of cities and arranged it in ascending order. Plotted it using “states.plot” with kind “barh” i.e. bar in horizontal. Then we gave label as No of cities for analysis and finally shown the plot using plt.show().
Now, we will introduce other columns to the datasets namely, latitude and longitude to get respective terms from the location column.
cities['latitude'] = cities['location'].apply(lambda x: x.split(',')) cities['longitude'] = cities['location'].apply(lambda x: x.split(','))
Then, we will get the list of items we are going to show on the map i.e. list of top 10 cities population vise.
print("The Top 10 Cities sorted according to the Total Population (Descending Order)") top_pop_cities = cities.sort_values(by='population_total',ascending=False) top10_pop_cities=top_pop_cities.head()
Plotting the Map:
And finally, here we will do what we are waiting for i.e. the map plotting part.
plt.subplots(figsize=(20, 15)) map = Basemap(width=1200000,height=900000,projection='lcc',resolution='l', llcrnrlon=67,llcrnrlat=5,urcrnrlon=99,urcrnrlat=37,lat_0=28,lon_0=77) map.drawmapboundary () map.drawcountries () map.drawcoastlines () lg=array(top10_pop_cities['longitude']) lt=array(top10_pop_cities['latitude']) pt=array(top10_pop_cities['population_total']) nc=array(top10_pop_cities['name_of_city']) x, y = map(lg, lt) population_sizes = top10_pop_cities["population_total"].apply(lambda x: int(x / 5000)) plt.scatter(x, y, s=population_sizes, marker="o", c=population_sizes, cmap=cm.Dark2, alpha=0.7) for ncs, xpt, ypt in zip(nc, x, y): plt.text(xpt+60000, ypt+30000, ncs, fontsize=10, fontweight='bold') plt.title('Top 10 Populated Cities in India',fontsize=20)
First, we will define a plot with a size 20×15. Then we will have a map using Basemap function. It requires width i.e. width of the required map in meters, height i.e. height of the required map in meters, used lambert conformal (‘lcc’) as the projection type, resolution will be low (‘l’). Also, we have used longitude of lower-left corner (llcrnrlon) to be 67, the latitude of lower-left corner (llcrnrlat) to be 5, the longitude of the upper right corner to be 99, the latitude of the upper right corner to be 37, central latitude line (lat_0) to be 28 and central meridian line (lon_0) to be 77, as per the dimensions of the map of India.
then used drawboundary, drawcountries and drawcoastlines to draw boundaries, countries and coastlines respectively.
And to print the required cities on the map we will store their longitude value, latitude values, population, and names in arrays. Now we will plot these latitude and longitude values on the map using ‘scatter’ and give them their corresponding names using “plt.text”. In last give the name to our map and here is the output.
After we run the above Python code, we will able to see the map of India just like you can see below: