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_us_ag_exports.csv')
for col in df.columns:
df[col] = df[col].astype(str)
scl = [
[0.0, 'rgb(242,240,247)'],
[0.2, 'rgb(218,218,235)'],
[0.4, 'rgb(188,189,220)'],
[0.6, 'rgb(158,154,200)'],
[0.8, 'rgb(117,107,177)'],
[1.0, 'rgb(84,39,143)']
]
df['text'] = df['state'] + '<br>' + \
'Beef ' + df['beef'] + ' Dairy ' + df['dairy'] + '<br>' + \
'Fruits ' + df['total fruits'] + ' Veggies ' + df['total veggies'] + '<br>' + \
'Wheat ' + df['wheat'] + ' Corn ' + df['corn']
data = [go.Choropleth(
colorscale = scl,
autocolorscale = False,
locations = df['code'],
z = df['total exports'].astype(float),
locationmode = 'USA-states',
text = df['text'],
marker = go.choropleth.Marker(
line = go.choropleth.marker.Line(
color = 'rgb(255,255,255)',
width = 2
)),
colorbar = go.choropleth.ColorBar(
title = "Millions USD")
)]
layout = go.Layout(
title = go.layout.Title(
text = '2011 US Agriculture Exports by State<br>(Hover for breakdown)'
),
geo = go.layout.Geo(
scope = 'usa',
projection = go.layout.geo.Projection(type = 'albers usa'),
showlakes = True,
lakecolor = 'rgb(255, 255, 255)'),
)
fig = go.Figure(data = data, layout = layout)
py.iplot(fig, filename = 'd3-cloropleth-map')
import plotly.plotly as py
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')
data = [go.Choropleth(
locations = df['CODE'],
z = df['GDP (BILLIONS)'],
text = df['COUNTRY'],
colorscale = [
[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)"]
],
autocolorscale = False,
reversescale = True,
marker = go.choropleth.Marker(
line = go.choropleth.marker.Line(
color = 'rgb(180,180,180)',
width = 0.5
)),
colorbar = go.choropleth.ColorBar(
tickprefix = '$',
title = 'GDP<br>Billions US$'),
)]
layout = go.Layout(
title = go.layout.Title(
text = '2014 Global GDP'
),
geo = go.layout.Geo(
showframe = False,
showcoastlines = False,
projection = go.layout.geo.Projection(
type = 'equirectangular'
)
),
annotations = [go.layout.Annotation(
x = 0.55,
y = 0.1,
xref = 'paper',
yref = 'paper',
text = 'Source: <a href="https://www.cia.gov/library/publications/the-world-factbook/fields/2195.html">\
CIA World Factbook</a>',
showarrow = False
)]
)
fig = go.Figure(data = data, layout = layout)
py.iplot(fig, filename = 'd3-world-map')
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/2014_ebola.csv')
df.head()
cases = []
colors = ['rgb(239,243,255)','rgb(189,215,231)','rgb(107,174,214)','rgb(33,113,181)']
months = {6:'June',7:'July',8:'Aug',9:'Sept'}
for i in range(6,10)[::-1]:
cases.append(go.Scattergeo(
lon = df[ df['Month'] == i ]['Lon'], #-(max(range(6,10))-i),
lat = df[ df['Month'] == i ]['Lat'],
text = df[ df['Month'] == i ]['Value'],
name = months[i],
marker = go.scattergeo.Marker(
size = df[ df['Month'] == i ]['Value']/50,
color = colors[i-6],
line = go.scattergeo.marker.Line(width = 0)
),
) )
cases[0]['text'] = df[ df['Month'] == 9 ]['Value'].map('{:.0f}'.format).astype(str)+' '+\
df[ df['Month'] == 9 ]['Country']
cases[0]['mode'] = 'markers+text'
cases[0]['textposition'] = 'bottom center'
inset = [
go.Choropleth(
locationmode = 'country names',
locations = df[ df['Month'] == 9 ]['Country'],
z = df[ df['Month'] == 9 ]['Value'],
text = df[ df['Month'] == 9 ]['Country'],
colorscale = [[0,'rgb(0, 0, 0)'],[1,'rgb(0, 0, 0)']],
autocolorscale = False,
showscale = False,
geo = 'geo2'
),
go.Scattergeo(
lon = [21.0936],
lat = [7.1881],
text = ['Africa'],
mode = 'text',
showlegend = False,
geo = 'geo2'
)
]
layout = go.Layout(
title = go.layout.Title(
text = 'Ebola cases reported by month in West Africa 2014<br> \
Source: <a href="https://data.hdx.rwlabs.org/dataset/rowca-ebola-cases">\
HDX</a>'),
geo = go.layout.Geo(
resolution = 50,
scope = 'africa',
showframe = False,
showcoastlines = True,
showland = True,
landcolor = "rgb(229, 229, 229)",
countrycolor = "rgb(255, 255, 255)" ,
coastlinecolor = "rgb(255, 255, 255)",
projection = go.layout.geo.Projection(
type = 'equirectangular'
),
lonaxis = go.layout.geo.Lonaxis(
range= [ -15.0, -5.0 ]
),
lataxis = go.layout.geo.Lataxis(
range= [ 0.0, 12.0 ]
),
domain = go.layout.geo.Domain(
x = [ 0, 1 ],
y = [ 0, 1 ]
)
),
geo2 = go.layout.Geo(
scope = 'africa',
showframe = False,
showland = True,
landcolor = "rgb(229, 229, 229)",
showcountries = False,
domain = go.layout.geo.Domain(
x = [ 0, 0.6 ],
y = [ 0, 0.6 ]
),
bgcolor = 'rgba(255, 255, 255, 0.0)',
),
legend = go.layout.Legend(
traceorder = 'reversed'
)
)
fig = go.Figure(layout=layout, data=cases+inset)
py.iplot(fig, filename='West Africa Ebola cases 2014')
For the full county choropleth doc page checkout https://plotly.com/python/county-choropleth/
import plotly.plotly as py
import plotly.figure_factory as ff
import numpy as np
import pandas as pd
df_sample = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/laucnty16.csv')
df_sample['State FIPS Code'] = df_sample['State FIPS Code'].apply(lambda x: str(x).zfill(2))
df_sample['County FIPS Code'] = df_sample['County FIPS Code'].apply(lambda x: str(x).zfill(3))
df_sample['FIPS'] = df_sample['State FIPS Code'] + df_sample['County FIPS Code']
colorscale = ["#f7fbff", "#ebf3fb", "#deebf7", "#d2e3f3", "#c6dbef", "#b3d2e9", "#9ecae1",
"#85bcdb", "#6baed6", "#57a0ce", "#4292c6", "#3082be", "#2171b5", "#1361a9",
"#08519c", "#0b4083", "#08306b"
]
endpts = list(np.linspace(1, 12, len(colorscale) - 1))
fips = df_sample['FIPS'].tolist()
values = df_sample['Unemployment Rate (%)'].tolist()
fig = ff.create_choropleth(
fips = fips, values = values, scope = ['usa'],
binning_endpoints = endpts, colorscale = colorscale,
show_state_data = False,
show_hover = True, centroid_marker = {
'opacity': 0
},
asp = 2.9,
title = 'USA by Unemployment %',
legend_title = '% unemployed'
)
py.iplot(fig, filename = 'choropleth_full_usa')
The draw time for this plot will be slow for clients without much RAM.
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-choroplethplot/", width="100%", height="950px", frameBorder="0")
from IPython.display import IFrame
IFrame(src= "https://dash-simple-apps.plotly.host/dash-choroplethplot/code", width="100%", height=500, frameBorder="0")
See https://plotly.com/python/reference/#choropleth 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(
'Choropleth_maps.ipynb', 'python/choropleth-maps/', 'Choropleth Maps',
'How to make choropleth maps in Python with Plotly.',
title = 'Python Choropleth Maps | Plotly',
has_thumbnail='true', thumbnail='thumbnail/choropleth.jpg',
language='python',
display_as='maps', order=1, ipynb='~notebook_demo/55')
Collecting git+https://github.com/plotly/publisher.git
Cloning https://github.com/plotly/publisher.git to /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-req-build-yyuryex2
Building wheels for collected packages: publisher
Building wheel for publisher (setup.py) ... done
Stored in directory: /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-ephem-wheel-cache-m3irlu81/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
You are using pip version 19.0.3, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.