Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!
Plotly's Python package is updated frequently. Run pip install plotly --upgrade
to make sure you're using the latest version.
import plotly
plotly.__version__
'3.6.1'
To plot on Mapbox maps with Plotly you'll need a Mapbox account and a public Mapbox Access Token which you can add to your Plotly settings. If you're using a Chart Studio Enterprise server, please see additional instructions here: https://help.plot.ly/mapbox-atlas/.
import plotly.plotly as py
import plotly.graph_objs as go
# mapbox_access_token = 'ADD_YOUR_TOKEN_HERE'
data = [
go.Scattermapbox(
lat=['45.5017'],
lon=['-73.5673'],
mode='markers',
marker=go.scattermapbox.Marker(
size=14
),
text=['Montreal'],
)
]
layout = go.Layout(
autosize=True,
hovermode='closest',
mapbox=go.layout.Mapbox(
accesstoken=mapbox_access_token,
bearing=0,
center=go.layout.mapbox.Center(
lat=45,
lon=-73
),
pitch=0,
zoom=5
),
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='Montreal Mapbox')
import plotly.plotly as py
import plotly.graph_objs as go
# mapbox_access_token = 'ADD_YOUR_TOKEN_HERE'
data = [
go.Scattermapbox(
lat=['38.91427','38.91538','38.91458',
'38.92239','38.93222','38.90842',
'38.91931','38.93260','38.91368',
'38.88516','38.921894','38.93206',
'38.91275'],
lon=['-77.02827','-77.02013','-77.03155',
'-77.04227','-77.02854','-77.02419',
'-77.02518','-77.03304','-77.04509',
'-76.99656','-77.042438','-77.02821',
'-77.01239'],
mode='markers',
marker=go.scattermapbox.Marker(
size=9
),
text=["The coffee bar","Bistro Bohem","Black Cat",
"Snap","Columbia Heights Coffee","Azi's Cafe",
"Blind Dog Cafe","Le Caprice","Filter",
"Peregrine","Tryst","The Coupe",
"Big Bear Cafe"],
)
]
layout = go.Layout(
autosize=True,
hovermode='closest',
mapbox=go.layout.Mapbox(
accesstoken=mapbox_access_token,
bearing=0,
center=go.layout.mapbox.Center(
lat=38.92,
lon=-77.07
),
pitch=0,
zoom=10
),
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='Multiple Mapbox')
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
# mapbox_access_token = 'ADD_YOUR_TOKEN_HERE'
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/Nuclear%20Waste%20Sites%20on%20American%20Campuses.csv')
site_lat = df.lat
site_lon = df.lon
locations_name = df.text
data = [
go.Scattermapbox(
lat=site_lat,
lon=site_lon,
mode='markers',
marker=go.scattermapbox.Marker(
size=17,
color='rgb(255, 0, 0)',
opacity=0.7
),
text=locations_name,
hoverinfo='text'
),
go.Scattermapbox(
lat=site_lat,
lon=site_lon,
mode='markers',
marker=go.scattermapbox.Marker(
size=8,
color='rgb(242, 177, 172)',
opacity=0.7
),
hoverinfo='none'
)]
layout = go.Layout(
title='Nuclear Waste Sites on Campus',
autosize=True,
hovermode='closest',
showlegend=False,
mapbox=go.layout.Mapbox(
accesstoken=mapbox_access_token,
bearing=0,
center=go.layout.mapbox.Center(
lat=38,
lon=-94
),
pitch=0,
zoom=3,
style='light'
),
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='Nuclear Waste Sites on American Campuses')
Dash is an Open Source Python library which can help you convert plotly figures into a reactive, web-based application. Below is a simple example of a dashboard created using Dash. Its source code can easily be deployed to a PaaS.
from IPython.display import IFrame
IFrame(src= "https://dash-simple-apps.plotly.host/dash-scattermapboxplot/", width="100%", height="850px", frameBorder="0")
from IPython.display import IFrame
IFrame(src= "https://dash-simple-apps.plotly.host/dash-scattermapboxplot/code", width="100%", height=500, frameBorder="0")
See https://plotly.com/python/reference/#scattermapbox for more information and options!
from IPython.display import display, HTML
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
! pip install git+https://github.com/plotly/publisher.git --upgrade
import publisher
publisher.publish(
'mapbox.ipynb', 'python/scattermapbox/', 'Python Scatter Plots with Mapbox',
'How to make scatter plots on Mapbox maps in Python.',
title = 'Python Scatter Plots with Mapbox | Plotly',
name = 'Scatter Plots on Mapbox',
has_thumbnail='true', thumbnail='thumbnail/scatter-mapbox.jpg',
language='python', page_type='example_index', ipynb='~notebook_demo/261',
display_as='maps', order=7, mapbox_access_token = 'your access token'
)