GeoPlotting (Basic) ¶
In this post, we will discuss on how to plot data on geographical map (for example say Map of Nepal). The following are the key requirement before proceeding. ¶
- Basic knowledge of Pandas
- GeoJson file (I downloaded from https://github.com/Acesmndr/nepal-geojson/blob/master/generated-geojson/nepal-with-districts-acesmndr.geojson)
- Geopandas installed
In [ ]:
pip install geopandas
In [8]:
import pandas as pd, numpy as np
import geopandas as gpd
import matplotlib.pyplot as plt
In [7]:
gdata=gpd.read_file(r"E:\PythonTest\nepal-with-districts-acesmndr.geojson")#path in your computer where the geojson file is located
gdata
Out[7]:
DISTRICT | HQ | PROVINCE | geometry | |
---|---|---|---|---|
0 | JHAPA | BHADRAPUR | 1 | POLYGON ((88.18476 26.78257, 88.1889 26.77911,... |
1 | ILAM | ILAM | 1 | POLYGON ((87.99458 27.10597, 87.99458 27.10511... |
2 | PANCHTHAR | PHIDIM | 1 | POLYGON ((88.07314 27.43522, 88.07474 27.43235... |
3 | TAPLEJUNG | TAPLEJUNG | 1 | POLYGON ((87.66631 27.80776, 87.67083 27.80748... |
4 | MORANG | BIRATNAGAR | 1 | POLYGON ((87.59093 26.86007, 87.59154 26.86023... |
... | ... | ... | ... | ... |
72 | BAJURA | MARTADI | 7 | POLYGON ((81.5087 29.89597, 81.51166 29.89771,... |
73 | KANCHANPUR | BHIMDATTA | 7 | POLYGON ((80.50436 28.99524, 80.50379 28.98816... |
74 | DADELDHURA | AMARGADHI | 7 | POLYGON ((80.78516 29.36439, 80.78385 29.36411... |
75 | BAITADI | DASHARATHCHAND | 7 | POLYGON ((80.41054 29.6032, 80.41209 29.60366,... |
76 | DARCHULA | DARCHULA | 7 | POLYGON ((81.09295 30.05481, 81.09427 30.05423... |
77 rows × 4 columns
As we can see, Geojson is nothing more than a dataframe with co-ordinates of the location. ¶
To plot data in this map (Districtwise), we need data with exact same district name. to make this process simple and super short, I ccreated a new column in GeoDataFrame (gdata) and filled random number between 50,100
In [9]:
gdata['Data']=np.random.randint(50, 101, size=len(gdata))
gdata
Out[9]:
DISTRICT | HQ | PROVINCE | geometry | Data | |
---|---|---|---|---|---|
0 | JHAPA | BHADRAPUR | 1 | POLYGON ((88.18476 26.78257, 88.1889 26.77911,... | 59 |
1 | ILAM | ILAM | 1 | POLYGON ((87.99458 27.10597, 87.99458 27.10511... | 65 |
2 | PANCHTHAR | PHIDIM | 1 | POLYGON ((88.07314 27.43522, 88.07474 27.43235... | 67 |
3 | TAPLEJUNG | TAPLEJUNG | 1 | POLYGON ((87.66631 27.80776, 87.67083 27.80748... | 65 |
4 | MORANG | BIRATNAGAR | 1 | POLYGON ((87.59093 26.86007, 87.59154 26.86023... | 54 |
... | ... | ... | ... | ... | ... |
72 | BAJURA | MARTADI | 7 | POLYGON ((81.5087 29.89597, 81.51166 29.89771,... | 69 |
73 | KANCHANPUR | BHIMDATTA | 7 | POLYGON ((80.50436 28.99524, 80.50379 28.98816... | 82 |
74 | DADELDHURA | AMARGADHI | 7 | POLYGON ((80.78516 29.36439, 80.78385 29.36411... | 69 |
75 | BAITADI | DASHARATHCHAND | 7 | POLYGON ((80.41054 29.6032, 80.41209 29.60366,... | 100 |
76 | DARCHULA | DARCHULA | 7 | POLYGON ((81.09295 30.05481, 81.09427 30.05423... | 93 |
77 rows × 5 columns
Lets Plot This ¶
In [11]:
gdata.plot("Data",legend=True)
Out[11]:
<Axes: >
Now Remaining tasks are further customization of the chart. ¶
In [15]:
gdata.plot("Data",legend=True,cmap="Blues",figsize=(10,6))
Out[15]:
<Axes: >
In [ ]:
can directly be written as gdata=gpd.read_file(r"https://github.com/Acesmndr/nepal-geojson/blob/master/generated-geojson/nepal-with-districts-acesmndr.geojson")
ReplyDelete