Geographical plotting of maps with Plotly | by Jenny Dcruz | Towards Data Science

Geographical plotting is a method of displaying data on a global scale as well as for states of a country, often in a colorful manner. It plays a significant role in data analysis and visualization. It is commonly used while building dashboards to present widespread data.
In order to create interactive maps we use python’s plotly library. Plotly is used to make interactive graphs, as well as create other visualizations.
Click here to learn more about plotly.
Let’s split this tutorial into 2 parts. In the first part we’ll focus on geographically plotting data based on a country, which in our case will be the agricultural export of the USA. In the 2nd part we’ll use a random, global GDP dataset. Both these datasets can be found here.
Basic understanding of python programming. Specifically python’s matplotlib and plotly libraries.
Begin by importing the following modules:
import pandas as pdimport chart_studio.plotly as pyimport plotly.offline as poimport plotly.graph_objs as pgimport matplotlib.pyplot as plt%matplotlib inline
You need to initiate plotly’s notebook mode to display the plot inside the notebook. This will allow you to generate graphs offline and save them in a local machine.
This can be done as follows:
po.init_notebook_mode(connected = True)
We load our first dataset based on the USA’s agricultural export.
AE = pd.read_csv(‘AgriculturalExport.csv’)AE.head()
Here, the code of the state is vital as it helps us with the location when we plot a geographical graph.
While plotting data, you need to consider a few factors such as : What kind of output do you need? Which locations are to be displayed? What are the values required to be displayed in every location? Last but not least, what kind of color, styling or text is required? All these basic values are under our data variable that is a dictionary. You can use different map types. Here we’re using choropleth which is a basic graph that does not require any other libraries. Then we have the locations i.e. the location codes. Next, we require locationmode which tells the method we plan on specifying the location. In our example, we set the location mode as usa-states. Now we need values for the locations and then the text to be displayed while hovering over each of the mentioned locations.
data = dict(type = ‘choropleth’,  locations = [‘AL’, ‘AK’, ‘AR’, ‘CA’],  locationmode = ‘USA-states’,  z = [1,2,30,40,50],  text = [‘alabama’, ‘alaska’, ‘arizona’, ‘pugger’, ‘california’])
In the layout variable we just need scope which is represented with geo. Make sure USA is not written in capitals as that will lead to an error.
layout = dict(geo = {'scope':'usa'})
The x variable uses the plotly.graph_objs module that we imported earlier in order to plot the structure of the state. In our case, that’s the USA. We pass 2 parameters to x: data and layout. Data represents the data represented on particular scopes; Layout defines what kind of scope we are using there.
x = pg.Figure(data = [data] ,layout = layout)po.iplot(x)
Now we just need to change the layout and data according to the values available in our dataset. Instead of typing each state’s code, we write AE[‘code’] which calls the state codes from our dataset. Similarly, z will represent the total exports for every state as shown below. Coming to the text variable, our dataset includes a column called text that displays all the data with regards to the agricultural export for every state so instead of changing the text variable to just the state names we write AE[‘text’]. Thus displaying all the export information for every state.
data = dict(type = 'choropleth',             locations = AE['code'],             locationmode = 'USA-states',             z = AE['total exports'],             text = AE['text'])
As for layout, we’re now talking about more than just the scope so we can add showlakes and a title as follows:
layout = dict(title = 'USAs Agricultural Exports',               geo = dict(scope = 'usa' ,                          showlakes = True,                          lakecolor = 'rgb(0,191,255)'))x = pg.Figure(data = [data],               layout = layout)po.iplot(x)
You can use different color scales like Portland, Greens, etc and even add a color bar to your data as shown below:
data = dict(type = 'choropleth',             locations = AE['code'],             locationmode = 'USA-states',             z = AE['total exports'],             text = AE['text'],             colorscale = 'Greens',             colorbar = {'title' : 'colorbar'})layout = dict(title = 'USAs Agricultural Exports with the lakes',               geo = dict(scope='usa' ,                        showlakes = True,                        lakecolor = 'rgb(0,191,255)'))x = pg.Figure(data = [data],               layout = layout)po.iplot(x)
We’ve now reached the second part of our article where we’ll plot geographical charts on a global scale. For this we’ll use our global GDP dataset.
G_GDP = pd.read_csv('GlobalGDP.csv')G_GDP.head()
So again, let’s define the data and layout.
data = dict(type='choropleth',             locations = G_GDP['CODE'],             z = G_GDP['GDP (BILLIONS)'],             text = G_GDP['COUNTRY'])
As we’re plotting a map on a global scale, layout will now consist of the projections attribute and not scope.
layout = dict(title = 'Global GDP - hammer projection',              geo = dict( projection = {'type':'hammer'},                         showlakes = True,                          lakecolor = 'rgb(0,191,255)'))x = pg.Figure(data = [data],               layout = layout)po.iplot(x)
What’s fun with these plots is that you can zoom into, swipe through, and view the data plotted around the globe conveniently. This is because of our projection type: hammer. We can view the same data using other projection types as well such as natural earth, robinson, mercator, stereographic etc. The code and output for few of these projection types are given below.
layout = dict(title = 'Global GDP - robinson projection',               geo = dict( projection = {'type':'robinson'},                          showlakes = True,                          lakecolor = 'rgb(0,191,255)'))x = pg.Figure(data = [data],               layout = layout) po.iplot(x)
layout = dict(title = 'Global GDP - natural earth projection',               geo = dict( projection = {'type':'natural earth'},                          showlakes = True,                          lakecolor = 'rgb(0,191,255)'))x = pg.Figure(data = [data],               layout = layout)po.iplot(x)
layout = dict(title = 'Global GDP - mercator projection',               geo = dict(projection = {'type':'mercator'},                        showlakes = True,                        lakecolor = 'rgb(0,191,255)'))x = pg.Figure(data = [data],               layout = layout)po.iplot(x)
layout = dict(title = 'Global GDP - stereographic projection',               geo = dict(projection = {'type':'stereographic'},                        showlakes = True,                        lakecolor = 'rgb(0,191,255)'))x = pg.Figure(data = [data],               layout = layout)po.iplot(x)
layout = dict(title = 'Global GDP - orthographic projection',               geo = dict( projection = {'type':'orthographic'},                        showlakes = True,                        lakecolor = 'rgb(0,191,255)'))x = pg.Figure(data = [data],               layout = layout)po.iplot(x)
You can remove the boundaries to your projections as follows:
layout = dict(title = 'Global GDP - robinson projection without borders',               geo = dict(showframe = False,                        projection = {'type':'robinson'},                        showlakes = True,                        lakecolor = 'rgb(0,191,255)'))x = pg.Figure(data = [data], layout = layout)po.iplot(x)
To learn more about Plotly's map configurations and styling you can refer to its documentation.
The whole code to this article can be found here.
I hope this quick tutorial was helpful. Thank you for giving it a read!
You can reach out to me here.