Choropleth Map of Tunisia with GeoViews

I will show here how to draw a Choropleth Map of Tunisia using GeoViews and we will represent the Regional Development Index in each gouvernorat in 2010 (Provinces of Tunisia).

You need first to upload in your working directory all the files in the my Github repository https://github.com/malouche/Map-Tunisia-Gouvernorat-HASC

let us first import the needed libraries

In [1]:
import pandas as pd
import geopandas as gpd
import geoviews as gv

gv.extension('bokeh')

We will now read the file that contains the boundaries of the Gouvernorats of Tunisia

In [2]:
geometries = gpd.read_file('TUN_adm1.shp')
In [3]:
geometries.boundary
Out[3]:
0     LINESTRING (9.826991000000021 36.9688349999999...
1     (LINESTRING (11.45263900000009 33.174863000000...
2     (LINESTRING (11.02347100000014 35.643750000000...
3     (LINESTRING (10.50375000000008 36.732361000000...
4     (LINESTRING (10.27680500000008 34.322082000000...
5     LINESTRING (9.44495999999998 35.50318900000002...
6     LINESTRING (9.536098000000152 36.4738209999999...
7     (LINESTRING (10.509861 36.03458300000011, 10.5...
8     (LINESTRING (10.21097000000009 37.093472000000...
9     LINESTRING (9.087803000000008 37.1587490000000...
10    LINESTRING (10.29486100000014 36.8104170000001...
11    (LINESTRING (10.23347200000012 37.162639000000...
12    LINESTRING (9.678669000000127 34.3066790000000...
13    LINESTRING (8.962442000000067 34.7113490000001...
14    LINESTRING (8.943959999999947 37.0022820000000...
15    LINESTRING (10.19923 36.16115300000001, 10.192...
16    LINESTRING (8.736524000000145 35.7821570000001...
17    LINESTRING (9.268528999999944 34.1957700000001...
18    LINESTRING (8.93204899999995 36.43988000000013...
19    (LINESTRING (11.09680600000002 35.157638000000...
20    LINESTRING (10.57399100000015 33.2556610000000...
21    LINESTRING (8.14167000000009 34.5122300000001,...
22    LINESTRING (10.23097200000001 36.9576380000000...
23    LINESTRING (9.993926000000044 36.6236989999999...
dtype: object
In [4]:
geometries.as_matrix
Out[4]:
<bound method NDFrame.as_matrix of     ID_0  ISO   NAME_0  ID_1                 NAME_1  \
0    225  TUN  Tunisia  2955                Manubah   
1    225  TUN  Tunisia  2956               Médenine   
2    225  TUN  Tunisia  2957               Monastir   
3    225  TUN  Tunisia  2958                 Nabeul   
4    225  TUN  Tunisia  2959                   Sfax   
5    225  TUN  Tunisia  2960           Sidi Bou Zid   
6    225  TUN  Tunisia  2961                Siliana   
7    225  TUN  Tunisia  2962                 Sousse   
8    225  TUN  Tunisia  2943                 Ariana   
9    225  TUN  Tunisia  2944                   Béja   
10   225  TUN  Tunisia  2945  Ben Arous (Tunis Sud)   
11   225  TUN  Tunisia  2946                Bizerte   
12   225  TUN  Tunisia  2947                  Gabès   
13   225  TUN  Tunisia  2948                  Gafsa   
14   225  TUN  Tunisia  2949               Jendouba   
15   225  TUN  Tunisia  2950               Kairouan   
16   225  TUN  Tunisia  2951              Kassérine   
17   225  TUN  Tunisia  2952                 Kebili   
18   225  TUN  Tunisia  2953                 Le Kef   
19   225  TUN  Tunisia  2954                 Mahdia   
20   225  TUN  Tunisia  2963              Tataouine   
21   225  TUN  Tunisia  2964                 Tozeur   
22   225  TUN  Tunisia  2965                  Tunis   
23   225  TUN  Tunisia  2966               Zaghouan   

                                            VARNAME_1 NL_NAME_1 HASC_1  CC_1  \
0                                                None      None  TN.MN  None   
1                           Madaniyin|Madanin|Medenin      None  TN.ME  None   
2                                         Al Munastir      None  TN.MS  None   
3                           Cap Bon|Nabil|Nabol|Nabul      None  TN.NB  None   
4                              Safaqis|?afaqis|?faqis      None  TN.SF  None   
5   Sidi Bu Zayd|Qamudah|Sidi Boû Sa`îd|Sidi Buzid...      None  TN.SZ  None   
6                                   Silianah|Silyanah      None  TN.SL  None   
7                        Sousa|Sussa|Susse|Susa|Susah      None  TN.SS  None   
8        Al Aryianah|L'Ariana|Tunis Ariana|Al Aryanah      None  TN.AN  None   
9                                          Bajah|Béja      None  TN.BJ  None   
10                Bin `Arus|Ben Arous|Tunis Ben Arous      None  TN.BA  None   
11     BanzartBanzart|Bensert|Binzart|Biserta|Bizerta      None  TN.BZ  None   
12                                        Qabis|Gabas      None  TN.GB  None   
13                                      Gaf?ah|Qaf?ah      None  TN.GF  None   
14  Jendoûbah|Jenduba|Jondouba|Jundubah|Souk-El-Ar...      None  TN.JE  None   
15    Al Qayrawan|Al Qirwan|Qairouân|Kairuã|Kairouwan      None  TN.KR  None   
16  Al Qa?rayn|Al Ga?rin|Al Qasrin|Kasserim|Al Qa?...      None  TN.KS  None   
17                               Kebilli|Qbili|Qibili      None  TN.KB  None   
18                              Al Kaf|El Kef|Kaf|Kef      None  TN.KF  None   
19                     Al Madiyah|Al Mahdiyya|Mahdiâh      None  TN.MH  None   
20          Foum Tataouine|Tatahouine|Tatuine|Tatawin      None  TN.TA  None   
21                                Tawzar|Touzar|Tuzar      None  TN.TO  None   
22   Tounis|Tunis City|Tunus|Túnez|Túnis|Tunisi|Tunis      None  TN.TU  None   
23        Tunis South|Zachouan|Zaguan|Zaghwan|Saghuan      None  TN.ZA  None   

     TYPE_1    ENGTYPE_1 VALIDFR_1 VALIDTO_1 REMARKS_1  Shape_Leng  \
0   Wilayat  Governorate    200009   Present      None    1.747941   
1   Wilayat  Governorate      1981   Present      None   12.475052   
2   Wilayat  Governorate  19740309   Present      None    2.306264   
3   Wilayat  Governorate  19730524   Present      None    3.969903   
4   Wilayat  Governorate  19740309   Present      None    8.180811   
5   Wilayat  Governorate  19731204   Present      None    5.043545   
6   Wilayat  Governorate  19740605   Present      None    4.424006   
7   Wilayat  Governorate  19560631   Present      None    3.286390   
8   Wilayat  Governorate      1986   Present      None    1.607265   
9   Wilayat  Governorate  19740605   Present      None    3.600448   
10  Wilayat  Governorate      1986   Present      None    1.695965   
11  Wilayat  Governorate  19730524   Present      None    5.338602   
12  Wilayat  Governorate      1981   Present      None    4.393111   
13  Wilayat  Governorate      1981   Present      None    4.439829   
14  Wilayat  Governorate  19560631   Present      None    3.376254   
15  Wilayat  Governorate  19560631   Present      None    4.284793   
16  Wilayat  Governorate  19731204   Present      None    4.447110   
17  Wilayat  Governorate      1981   Present      None    7.089867   
18  Wilayat  Governorate  19740605   Present      None    3.401512   
19  Wilayat  Governorate  19740309   Present      None    3.898214   
20  Wilayat  Governorate      1981   Present      None   10.780472   
21  Wilayat  Governorate      1981   Present      None    3.916047   
22  Wilayat  Governorate      1986   Present      None    1.628017   
23  Wilayat  Governorate      1981   Present      None    2.676174   

    Shape_Area                                           geometry  
0     0.102080  POLYGON ((9.826991000000021 36.96883499999996,...  
1     0.836014  (POLYGON ((11.45263900000009 33.17486300000007...  
2     0.098287  (POLYGON ((11.02347100000014 35.64375000000013...  
3     0.287665  (POLYGON ((10.50375000000008 36.73236100000008...  
4     0.711881  (POLYGON ((10.27680500000008 34.32208200000002...  
5     0.728026  POLYGON ((9.44495999999998 35.50318900000002, ...  
6     0.469822  POLYGON ((9.536098000000152 36.47382099999999,...  
7     0.267447  (POLYGON ((10.509861 36.03458300000011, 10.509...  
8     0.055035  (POLYGON ((10.21097000000009 37.09347200000002...  
9     0.353934  POLYGON ((9.087803000000008 37.15874900000006,...  
10    0.069791  POLYGON ((10.29486100000014 36.81041700000014,...  
11    0.343080  (POLYGON ((10.23347200000012 37.16263900000007...  
12    0.726904  POLYGON ((9.678669000000127 34.30667900000009,...  
13    0.724787  POLYGON ((8.962442000000067 34.7113490000001, ...  
14    0.307828  POLYGON ((8.943959999999947 37.00228200000004,...  
15    0.658624  POLYGON ((10.19923 36.16115300000001, 10.19247...  
16    0.797627  POLYGON ((8.736524000000145 35.78215700000015,...  
17    2.118575  POLYGON ((9.268528999999944 34.1957700000001, ...  
18    0.503380  POLYGON ((8.93204899999995 36.43988000000013, ...  
19    0.290958  (POLYGON ((11.09680600000002 35.15763800000002...  
20    3.773432  POLYGON ((10.57399100000015 33.25566100000003,...  
21    0.599178  POLYGON ((8.14167000000009 34.5122300000001, 8...  
22    0.027300  POLYGON ((10.23097200000001 36.95763800000009,...  
23    0.286755  POLYGON ((9.993926000000044 36.62369899999999,...  >

We now import the data containing the variable that will be represented in the map and the column HASC_1 that already exists in the geometries data obtained after importing the shapefile.

In [5]:
idr=pd.read_csv('idr_gouv.csv')
In [6]:
idr.columns
Out[6]:
Index(['HASC_1', 'gouvernorat', 'IDR'], dtype='object')

We need then to merge both data

In [7]:
gdf = gpd.GeoDataFrame(pd.merge(geometries, idr))
In [8]:
gdf.head()
Out[8]:
ID_0 ISO NAME_0 ID_1 NAME_1 VARNAME_1 NL_NAME_1 HASC_1 CC_1 TYPE_1 ENGTYPE_1 VALIDFR_1 VALIDTO_1 REMARKS_1 Shape_Leng Shape_Area geometry gouvernorat IDR
0 225 TUN Tunisia 2955 Manubah None None TN.MN None Wilayat Governorate 200009 Present None 1.747941 0.102080 POLYGON ((9.826991000000021 36.96883499999996,... MANOUBA 0.53
1 225 TUN Tunisia 2956 Médenine Madaniyin|Madanin|Medenin None TN.ME None Wilayat Governorate 1981 Present None 12.475052 0.836014 (POLYGON ((11.45263900000009 33.17486300000007... MEDENINE 0.50
2 225 TUN Tunisia 2957 Monastir Al Munastir None TN.MS None Wilayat Governorate 19740309 Present None 2.306264 0.098287 (POLYGON ((11.02347100000014 35.64375000000013... MONASTIR 0.64
3 225 TUN Tunisia 2958 Nabeul Cap Bon|Nabil|Nabol|Nabul None TN.NB None Wilayat Governorate 19730524 Present None 3.969903 0.287665 (POLYGON ((10.50375000000008 36.73236100000008... NABEUL 0.57
4 225 TUN Tunisia 2959 Sfax Safaqis|?afaqis|?faqis None TN.SF None Wilayat Governorate 19740309 Present None 8.180811 0.711881 (POLYGON ((10.27680500000008 34.32208200000002... SFAX 0.56

We are ready now to draw the choropleth of Tunisia.

In [9]:
plot_opts = dict(tools=['hover'], width=550, height=800, color_index='IDR',
                 colorbar=True, toolbar=None, xaxis=None, yaxis=None)
gv.Polygons(gdf, vdims=['gouvernorat', 'IDR'], label='Regional Development Index in Tunisia (2010)').opts(plot=plot_opts)
Out[9]:

Let's now save the map into an html file

In [10]:
renderer = gv.renderer('bokeh')
In [11]:
g_idr=gv.Polygons(gdf, vdims=['gouvernorat', 'IDR'], label='Regional Development Index in Tunisia').opts(plot=plot_opts)
In [12]:
renderer.save(g_idr, 'g_idr')