Let us learn how we can accomplish this.
Keep in mind that you may require a Map box account or a Map box access token to use the API. You can sign up for the Map box on the official page.”
Function Syntax and Parameter List
The function syntax and parameter list are as shown below:
text=None, hover_name=None, hover_data=None, custom_data=None,
line_group=None, animation_frame=None, animation_group=None,
category_orders=None, labels=None, color_discrete_sequence=None,
color_discrete_map=None, zoom=8, center=None, mapbox_style=None, title=None,
template=None, width=None, height=None)
The following is a list of the most common parameters you will use when working with this function.
- data_frame – defines the DataFrame containing the column names used in the plot.
- lat – sets the values used to position the marks on the latitude on the map.
- lon – similar to the lat parameter, but the values are used to position the marks on the longitude on the map.
- color – specifies the values used to assign a unique color to the marks.
- zoom – sets the map zoom level. Starts from 0 to 20.
- title – sets the title for the figure.
- width/height – defines the width and height of the figure in pixels.
Example
Let us use the line_mapbox function to create lines on a map using the US Cities dataset. You can download the dataset in the resource below:
https://simplemaps.com/data/us-cities
The code is as shown below:
import pandas as pd
px.set_mapbox_access_token(open('mapbox.mapbox_token').read())
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
us_cities = df.query("State in ['New York', 'Pennsylvania']")
fig = px.line_mapbox(us_cities, lat="lat", lon="lon")
fig.show()
The code above will use the mapbox API to create lines of the cities in the New York and Pennsylvania states.
You can also set the default zoom level using the zoom parameter:
fig.show()
This should reset the default zoom level to 1.
Closing
That’s it for this one. Feel free to explore the mapbox and Plotly docs for more.