Choropleth Map using Plotly: Geographical Visualization
Published: 2023-01-31 11:33:32

We will discuss the followings-
**1. Structure**
**2. Nationwise example
-Without chart
-With chart**
**3. Global example**
#<u>Stucture:</u>
First we need to-
```
import pandas as pd
import chart_studio.plotly as py
import plotly.graph_objs as go
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)

```
It might seem really complex...so let's break it down into steps-
###Step-1:
We need to begin to build our data dictionary. Easiest way to do this is to use the `dict()` function of the general form:
![02](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c9dff8t4mecbjk4uohfh.png)
[Custom colorscale](https://plotly.com/python/colorscales/)
locationmode can be: USA|ISO-3|country names
location and locationmode are connected
For details [check this](https://plotly.com/python-api-reference/generated/plotly.express.choropleth.html) 
###Step-2:
Then we create the layout nested dictionary:
![03](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oaoa27mso905hp2r36bb.png)
The 'scope' property is an enumeration that may be specified as:
- One of the following enumeration value:['africa', 'asia', 'europe', 'north america', 'south america', 'usa', 'world']

###Step-3:
Then we use:
![04](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5h3tv2vsdxtgqfpo4n6.png)
to set up the object that finally gets passed into iplot()
###Step-4:
![05](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gzx1vs001mj727a9rqm.png)
To know all the dimensions, arguments1 and editing options [check this](https://plotly.com/python-api-reference/generated/plotly.express.choropleth.html)

#<u>Nationwise example:</u>
##Without chart:
Let's plot USA states-
![06](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8xbs8b9xgfjbyjewbdzl.png)
![07](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kqzwuj2ib9eadz3enl4.png)

##With chart:
For this a statistical chart of nationwise states has to be present in your repository.
![08](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzr08pjvr7lldbas2wg9.png)
We will use the columns for 'location', 'text', 'z' etc.
![09](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fksn7mjkbfqjq2m1g2i.png)
![10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8tbajvkoz1zjk89ih10z.png)
Here we used some customization.

#<u>Global Example:</u>
Here we will have to include a statistical chart that includes all the countries in the world

![13](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndivma3uqwim7gz8l33j.png)

![11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1d3oad7ox21itk470h1l.png)

![12](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1abzpyqto6w0af86oj02.png)
We can plot different types of maps by changing the projection

![14](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zu62o4aqn9m28iawif5h.png)
![15](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbh136li34igz0crrsom.png)
![16](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1pb1hfrjxhb0zkh8h7qo.png)
So we have lots of customization option...Don't forget to reference the documentation....
**(!)**Wanna see the choropleth map of Bangladesh...see you soon...let's dive😉

<u>Important Links:</u>
- [Documentation](https://plotly.com/python-api-reference/generated/plotly.graph_objects.Choropleth.html#plotly.graph_objects.Choropleth)
- [Colorscale](https://plotly.com/python/colorscales/)