plotly

Plotly.Express.Line_mapbox

“Plotly allows us to create lines on a map using the Map box API and Plotly Express module.
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:

plotly.express.line_mapbox(data_frame=None, lat=None, lon=None, color=None,

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.

  1. data_frame – defines the DataFrame containing the column names used in the plot.
  2. lat – sets the values used to position the marks on the latitude on the map.
  3. lon – similar to the lat parameter, but the values are used to position the marks on the longitude on the map.
  4. color – specifies the values used to assign a unique color to the marks.
  5. zoom – sets the map zoom level. Starts from 0 to 20.
  6. title – sets the title for the figure.
  7. 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 plotly.express as px
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 = px.line_mapbox(us_cities, lat="lat", lon="lon", zoom=1)
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.

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