Visualization of map information with Plotly + Mapbox

Yuki Wakatsuki
3 min readJan 4, 2022

--

Photo by Andrew Stutesman on Unsplash

In many fields, there is a need to use geographic information in daily operations. Many industries such as aviation, transportation, real estate, civil engineering, shipping, researchers, and consulting use GIS. In these industries, GIS has become an essential infrastructure for visualizing geographic information to understand spatial location and for analysis, forecasting, and planning.

In this article, I will introduce how to visualize map information using Mapbox, which is supported in Python within the Plotly library for visualizing geographical information. I will instroduce how to visualize base geographic map with Plotly + Mapbox.

0.Preprocess

At first, you have to install Plotly in your Python environment if you haven’t already installed it. You can install Plotly as below for simplely.

pip install plotly
  1. Visualize base map

You can plot geographic map easily as below.

import plotly.graph_objects as gofig = go.Figure()
fig.add_trace(go.Scattermapbox())
fig.update_layout(margin ={'l':0,'t':0,'b':0,'r':0},
mapbox = {
'center': {'lon': 139, 'lat': 36.5},
'style': "stamen-terrain",
'zoom': 4.5},
width=1600,
height=900,)
fig.show()
Photo by Yuki Wakatsuki on Github

Now, I visualized the geographic map around Japan.

a. Define graph object instancefigas plotly.graph_objects.Figure().

b. Add graph object to instance figwith fig.add_trace(). Here, I added plotly.graph_objects.Scattermapbox(). This the Mapbox object which is supported within Plotly. With using this object, we can use Mapbox’s geographic map.

c. Adjust map parameters with fig.update_layout(). I added 4 parameters in this object, margin, mapbox, width, height. margin means that the margin size of the graph area. mapbox parameter can set the ‘center’(center coodinate position), ‘style’(map design style), ‘zoom’(zoom size value).

And, this figure can be saved as .html, then you can see map interactively like adjusting zoom ratio. You can check the interactive figure at the link of below figure.

These are the base settings of first visualizing map with Plotly + Mapbox. Wirh only changing these parameters, you can customize forwhat you want. Next, I will show the variation with only using these settings.

2. Customize map

‘center’: {‘lon’: 118, ‘lat’: 18},
Photo by Yuki Wakatsuki on Github
‘center’: {‘lon’: 70, ‘lat’: 18},
Photo by Yuki Wakatsuki on Github
‘center’: {‘lon’: 15, ‘lat’: 46},
Photo by Yuki Wakatsuki on Github
‘center’: {‘lon’: 15, ‘lat’: 5},
Photo by Yuki Wakatsuki on Github
‘center’: {‘lon’: -100, ‘lat’: 40},
Photo by Yuki Wakatsuki on Github
‘center’: {‘lon’: -60, ‘lat’: -10},
Photo by Yuki Wakatsuki on Github
'style': "open-street-map",
Photo by Yuki Wakatsuki on Github
'style': "carto-positron",
Photo by Yuki Wakatsuki on Github
'style': "carto-darkmatter",
Photo by Yuki Wakatsuki on Github
'style': "stamen-toner",
Photo by Yuki Wakatsuki on Github
'style': "stamen-watercolor",
Photo by Yuki Wakatsuki on Github
'zoom': 3
Photo by Yuki Wakatsuki on Github
'zoom': 6
Photo by Yuki Wakatsuki on Github

In the future, I will introduce the methods how to plot information and actual data on map.

--

--

Yuki Wakatsuki

a data scientist. Tokyo, Japan. Interested in data science, data analysis, data visualization, statistics, machine learning, hydrology.