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 use the latest version.
import plotly
plotly.__version__
'3.6.1'
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
df.head()
df['text'] = df['airport'] + '' + df['city'] + ', ' + df['state'] + '' + 'Arrivals: ' + df['cnt'].astype(str)
scl = [ [0,"rgb(5, 10, 172)"],[0.35,"rgb(40, 60, 190)"],[0.5,"rgb(70, 100, 245)"],\
[0.6,"rgb(90, 120, 245)"],[0.7,"rgb(106, 137, 247)"],[1,"rgb(220, 220, 220)"] ]
data = [ go.Scattergeo(
locationmode = 'USA-states',
lon = df['long'],
lat = df['lat'],
text = df['text'],
mode = 'markers',
marker = dict(
size = 8,
opacity = 0.8,
reversescale = True,
autocolorscale = False,
symbol = 'square',
line = dict(
width=1,
color='rgba(102, 102, 102)'
),
colorscale = scl,
cmin = 0,
color = df['cnt'],
cmax = df['cnt'].max(),
colorbar=dict(
title="Incoming flights<br>February 2011"
)
))]
layout = dict(
title = 'Most trafficked US airports<br>(Hover for airport names)',
geo = dict(
scope='usa',
projection=dict( type='albers usa' ),
showland = True,
landcolor = "rgb(250, 250, 250)",
subunitcolor = "rgb(217, 217, 217)",
countrycolor = "rgb(217, 217, 217)",
countrywidth = 0.5,
subunitwidth = 0.5
),
)
fig = go.Figure(data=data, layout=layout )
py.iplot(fig, filename='d3-airports' )
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2015_06_30_precipitation.csv')
scl = [0,"rgb(150,0,90)"],[0.125,"rgb(0, 0, 200)"],[0.25,"rgb(0, 25, 255)"],\
[0.375,"rgb(0, 152, 255)"],[0.5,"rgb(44, 255, 150)"],[0.625,"rgb(151, 255, 0)"],\
[0.75,"rgb(255, 234, 0)"],[0.875,"rgb(255, 111, 0)"],[1,"rgb(255, 0, 0)"]
data = [go.Scattergeo(
lat = df['Lat'],
lon = df['Lon'],
text = df['Globvalue'].astype(str) + ' inches',
marker = dict(
color = df['Globvalue'],
colorscale = scl,
reversescale = True,
opacity = 0.7,
size = 2,
colorbar = dict(
thickness = 10,
titleside = "right",
outlinecolor = "rgba(68, 68, 68, 0)",
ticks = "outside",
ticklen = 3,
showticksuffix = "last",
ticksuffix = " inches",
dtick = 0.1
)
)
)]
layout = dict(
geo = dict(
scope = 'north america',
showland = True,
landcolor = "rgb(212, 212, 212)",
subunitcolor = "rgb(255, 255, 255)",
countrycolor = "rgb(255, 255, 255)",
showlakes = True,
lakecolor = "rgb(255, 255, 255)",
showsubunits = True,
showcountries = True,
resolution = 50,
projection = dict(
type = 'conic conformal',
rotation = dict(
lon = -100
)
),
lonaxis = dict(
showgrid = True,
gridwidth = 0.5,
range= [ -140.0, -55.0 ],
dtick = 5
),
lataxis = dict (
showgrid = True,
gridwidth = 0.5,
range= [ 20.0, 60.0 ],
dtick = 5
)
),
title = 'US Precipitation 06-30-2015<br>Source: <a href="http://water.weather.gov/precip/">NOAA</a>',
)
fig = go.Figure(data=data, layout=layout )
py.iplot(fig, filename='precipitation')
See https://plotly.com/python/reference/#scattergeo and https://plotly.com/python/reference/#layout-geo for more information and chart attribute 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(
'scatter-plot-on-map.ipynb', 'python/scatter-plots-on-maps/', 'Python Scatter Plots on Maps | Examples | Plotly',
'How to make scatter plots on maps in Python. Scatter plots on maps highlight geographic areas and can be colored by value.',
title = 'Python Scatter Plots on Maps | Plotly',
name = 'Scatter Plots on Maps',
has_thumbnail='true', thumbnail='thumbnail/scatter-plot-on-maps.jpg',
language='python',
display_as='maps', order=2,
ipynb= '~notebook_demo/57')
Collecting git+https://github.com/plotly/publisher.git Cloning https://github.com/plotly/publisher.git to c:\users\priyat~1\appdata\local\temp\pip-req-build-s9h9i_vt Building wheels for collected packages: publisher Running setup.py bdist_wheel for publisher: started Running setup.py bdist_wheel for publisher: finished with status 'done' Stored in directory: C:\Users\PRIYAT~1\AppData\Local\Temp\pip-ephem-wheel-cache-6ol6d604\wheels\99\3e\a0\fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966 Successfully built publisher Installing collected packages: publisher Found existing installation: publisher 0.13 Uninstalling publisher-0.13: Successfully uninstalled publisher-0.13 Successfully installed publisher-0.13
C:\Anaconda\Anaconda3\lib\site-packages\IPython\nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead. C:\Anaconda\Anaconda3\lib\site-packages\publisher\publisher.py:53: UserWarning: Did you "Save" this notebook before running this command? Remember to save, always save.