plotly

Plotly.Express.Set_mapbox_access_token

“Mapbox is one of the most popular providers of location data and maps API services. It is a popular provider of Map services, Geocoding API, Navigation data, and more.

If you are working with map plots, you are likely to use the Mapbox API to create various map plots.

In this article, we will guide you on how to set up your Plotly environment to use the Mapbox API services.”

Step 1 – Sign Up for MapBox Account
Before you can set up Plotly to work with Mapbox, you will need to create a MapBox account. This will give you access to a unique API you can use to create various types of map plots.

Open your browser and navigate to the resource below:

https://www.mapbox.com/

Click on Sign up to create a free account.

Enter your account details and proceed to create your account. Keep in mind that you will need to add billing details to proceed.

MapBox will not charge you until you need to access premium services.

Once completed, you will get access to your access token under your accounts page.

Copy this access token as we will need it in the next step.

Step 2 – Save the Access Token to File

The next step is to create a file to save your access token. It is recommended to save your access to a file instead of hard coding it into your projects. Saving the access token to a file means you can load the file when needed and exclude it from any public repository.

Create a file ending in .mapbox_token. Keep in mind that the extension is not required, but it is recommended to help you distinguish it from other files.

$ touch access.mapbox_token

Next, edit the file and paste the access token you copied earlier.

$ nano access.mapbox_token

Save and close the file.

Step 3 – Load Your MapBox Token Using Plotly Express

If you want to create a plot using the MapBox API, you will need to tell Plotly where the access token is located.

We do this using the set_mapbox_access_token() function. The function syntax is as shown:

plotly.express.set_mapbox_access_token(token)

To load the token saved in the file, we can run the code as:

import plotly.express as px
px.set_mapbox_access_token(open('access.mapbox_token').read())

In the code above, we start by importing Plotly express as px.

We then read the access token from the file using the open and read functions. Once read, we can then tell plotly to use that access token using the set_mapbox_access_token() function.

You can then use Plotly’s functions, such as scatter_mapbox, choropleth_mapbox, line_mapbox(), etc., to create various kinds of plots using the Mapbox API.

Ending

This article explores the basics of configuring the MapBox API on your Plotly projects using the plotly.express.set_mapbox_access_token() function.

Thanks for reading!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list