{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotly Express: a Walkthrough\n",
"\n",
"This notebook is the executable version of the example we walk through in our [Medium announcement article](https://medium.com/@plotlygraphs/introducing-plotly-express-808df010143d) introducing [Plotly Express](https://plotly.express): a terse, consistent, high-level wrapper around Plotly.py for rapid data exploration and figure generation.\n",
"\n",
"Plotly Express is now part of the main Plotly.py package, so to install it, just follow our [Getting Started guide](https://plot.ly/python/getting-started/) and remember to install the JupyterLab extensions if you're using JupyterLab, otherwise things won't work!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once you import Plotly Express (aka `px`), most plots are made with just one function call that accepts a [tidy Pandas data frame](http://www.jeannicholashould.com/tidy-data-in-python.html), and a simple description of the plot you want to make. For example if you want a simple scatter plot, it’s just `px.scatter(data, x=\"column_name\", y=\"column_name\")`. Here’s an example with the [Gapminder dataset](https://www.gapminder.org/tools/#$state$time$value=2007;;&chart-type=bubbles) – which comes built-in! – showing life expectancy vs GPD per capita by country for 2007:\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"hoverlabel": {
"namelength": 0
},
"hovertemplate": "gdpPercap=%{x}
lifeExp=%{y}",
"legendgroup": "",
"marker": {
"color": "#636efa",
"symbol": "circle"
},
"mode": "markers",
"name": "",
"showlegend": false,
"type": "scatter",
"x": [
974.5803384,
5937.029525999999,
6223.367465,
4797.231267,
12779.379640000001,
34435.367439999995,
36126.4927,
29796.048339999998,
1391.253792,
33692.60508,
1441.284873,
3822.1370840000004,
7446.298803,
12569.851770000001,
9065.800825,
10680.79282,
1217.032994,
430.07069160000003,
1713.7786859999999,
2042.0952399999999,
36319.235010000004,
706.016537,
1704.0637239999999,
13171.63885,
4959.1148539999995,
7006.580419,
986.1478792000001,
277.55185869999997,
3632.557798,
9645.06142,
1544.750112,
14619.222719999998,
8948.102923,
22833.30851,
35278.41874,
2082.4815670000003,
6025.374752000001,
6873.262326000001,
5581.180998,
5728.353514,
12154.08975,
641.3695236000001,
690.8055759,
33207.0844,
30470.0167,
13206.48452,
752.7497265,
32170.37442,
1327.60891,
27538.41188,
5186.050003,
942.6542111,
579.2317429999999,
1201.637154,
3548.3308460000003,
39724.97867,
18008.94444,
36180.789189999996,
2452.210407,
3540.6515640000002,
11605.71449,
4471.061906,
40675.99635,
25523.2771,
28569.7197,
7320.880262000001,
31656.06806,
4519.461171,
1463.249282,
1593.06548,
23348.139730000003,
47306.98978,
10461.05868,
1569.331442,
414.5073415,
12057.49928,
1044.770126,
759.3499101,
12451.6558,
1042.581557,
1803.1514960000002,
10956.99112,
11977.57496,
3095.7722710000003,
9253.896111,
3820.17523,
823.6856205,
944,
4811.060429,
1091.359778,
36797.93332,
25185.00911,
2749.320965,
619.6768923999999,
2013.9773050000001,
49357.19017,
22316.19287,
2605.94758,
9809.185636,
4172.838464,
7408.905561,
3190.481016,
15389.924680000002,
20509.64777,
19328.70901,
7670.122558,
10808.47561,
863.0884639000001,
1598.435089,
21654.83194,
1712.4721359999999,
9786.534714,
862.5407561000001,
47143.179639999995,
18678.31435,
25768.25759,
926.1410683,
9269.657808,
28821.0637,
3970.0954070000003,
2602.394995,
4513.480643,
33859.74835,
37506.419069999996,
4184.548089,
28718.27684,
1107.482182,
7458.3963269999995,
882.9699437999999,
18008.50924,
7092.923025,
8458.276384,
1056.3801210000001,
33203.26128,
42951.65309,
10611.46299,
11415.805690000001,
2441.576404,
3025.349798,
2280.769906,
1271.211593,
469.70929810000007
],
"xaxis": "x",
"y": [
43.828,
76.423,
72.301,
42.731,
75.32,
81.235,
79.829,
75.635,
64.062,
79.441,
56.728,
65.554,
74.852,
50.728,
72.39,
73.005,
52.295,
49.58,
59.723,
50.43,
80.653,
44.74100000000001,
50.651,
78.553,
72.961,
72.889,
65.152,
46.461999999999996,
55.321999999999996,
78.782,
48.328,
75.748,
78.273,
76.486,
78.332,
54.791000000000004,
72.235,
74.994,
71.33800000000001,
71.878,
51.57899999999999,
58.04,
52.946999999999996,
79.313,
80.657,
56.735,
59.448,
79.406,
60.022,
79.483,
70.259,
56.007,
46.388000000000005,
60.916000000000004,
70.19800000000001,
82.208,
73.33800000000001,
81.757,
64.69800000000001,
70.65,
70.964,
59.545,
78.885,
80.745,
80.546,
72.567,
82.603,
72.535,
54.11,
67.297,
78.623,
77.58800000000001,
71.993,
42.592,
45.678000000000004,
73.952,
59.443000000000005,
48.303000000000004,
74.241,
54.467,
64.164,
72.801,
76.195,
66.803,
74.543,
71.164,
42.082,
62.068999999999996,
52.906000000000006,
63.785,
79.762,
80.204,
72.899,
56.867,
46.858999999999995,
80.196,
75.64,
65.483,
75.53699999999999,
71.752,
71.421,
71.688,
75.563,
78.098,
78.74600000000001,
76.442,
72.476,
46.242,
65.528,
72.777,
63.062,
74.002,
42.568000000000005,
79.972,
74.663,
77.926,
48.159,
49.339,
80.941,
72.396,
58.556000000000004,
39.613,
80.884,
81.70100000000001,
74.143,
78.4,
52.516999999999996,
70.616,
58.42,
69.819,
73.923,
71.777,
51.542,
79.425,
78.242,
76.384,
73.747,
74.249,
73.422,
62.698,
42.38399999999999,
43.486999999999995
],
"yaxis": "y"
}
],
"layout": {
"height": 600,
"legend": {
"tracegroupgap": 0
},
"margin": {
"t": 60
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
},
"xaxis": {
"anchor": "y",
"domain": [
0,
1
],
"title": {
"text": "gdpPercap"
}
},
"yaxis": {
"anchor": "x",
"domain": [
0,
1
],
"title": {
"text": "lifeExp"
}
}
}
},
"text/html": [
"