Created
March 12, 2020 05:21
-
-
Save smmaurer/b3a7fe84fd88fbce76e8454ce091d0a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# MAG charts for workflow testing\n", | |
"\n", | |
"Notebook by Sam Maurer, Mar 2020.\n", | |
"\n", | |
"Prototype Plotly and Altair/Vega charts." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# hdf = pd.HDFStore('for_indicators.h5')\n", | |
"# print(hdf.keys())\n", | |
"# hdf.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[4811659, 5216528, 5617172, 6021824, 6411068, 6773367, 7117010, 7453472]\n" | |
] | |
} | |
], | |
"source": [ | |
"# Get population by year\n", | |
"\n", | |
"hdf = pd.HDFStore('for_indicators.h5')\n", | |
"\n", | |
"years = ['2020', '2025', '2030', '2035', '2040', '2045', '2050', '2055']\n", | |
"pop = [hdf.get_storer(y+'/persons').shape[0] for y in years]\n", | |
"\n", | |
"hdf.close()\n", | |
"print(pop)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Plotly\n", | |
"\n", | |
"https://plot.ly/python/ \n", | |
"https://plot.ly/javascript/" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import plotly.express as px" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.plotly.v1+json": { | |
"config": { | |
"plotlyServerURL": "https://plot.ly" | |
}, | |
"data": [ | |
{ | |
"alignmentgroup": "True", | |
"hoverlabel": { | |
"namelength": 0 | |
}, | |
"hovertemplate": "Simulation year=%{x}<br>Population=%{y}", | |
"legendgroup": "", | |
"marker": { | |
"color": "#636efa" | |
}, | |
"name": "", | |
"offsetgroup": "", | |
"orientation": "v", | |
"showlegend": false, | |
"textposition": "auto", | |
"type": "bar", | |
"x": [ | |
"2020", | |
"2025", | |
"2030", | |
"2035", | |
"2040", | |
"2045", | |
"2050", | |
"2055" | |
], | |
"xaxis": "x", | |
"y": [ | |
4811659, | |
5216528, | |
5617172, | |
6021824, | |
6411068, | |
6773367, | |
7117010, | |
7453472 | |
], | |
"yaxis": "y" | |
} | |
], | |
"layout": { | |
"barmode": "relative", | |
"height": 400, | |
"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 | |
} | |
} | |
}, | |
"title": { | |
"text": "Population by simulation year" | |
}, | |
"width": 600, | |
"xaxis": { | |
"anchor": "y", | |
"domain": [ | |
0, | |
1 | |
], | |
"title": { | |
"text": "Simulation year" | |
} | |
}, | |
"yaxis": { | |
"anchor": "x", | |
"domain": [ | |
0, | |
1 | |
], | |
"title": { | |
"text": "Population" | |
} | |
} | |
} | |
}, | |
"text/html": [ | |
"<div>\n", | |
" \n", | |
" \n", | |
" <div id=\"0bea95f8-eeca-4da1-995c-d0a38896972e\" class=\"plotly-graph-div\" style=\"height:400px; width:600px;\"></div>\n", | |
" <script type=\"text/javascript\">\n", | |
" require([\"plotly\"], function(Plotly) {\n", | |
" window.PLOTLYENV=window.PLOTLYENV || {};\n", | |
" \n", | |
" if (document.getElementById(\"0bea95f8-eeca-4da1-995c-d0a38896972e\")) {\n", | |
" Plotly.newPlot(\n", | |
" '0bea95f8-eeca-4da1-995c-d0a38896972e',\n", | |
" [{\"alignmentgroup\": \"True\", \"hoverlabel\": {\"namelength\": 0}, \"hovertemplate\": \"Simulation year=%{x}<br>Population=%{y}\", \"legendgroup\": \"\", \"marker\": {\"color\": \"#636efa\"}, \"name\": \"\", \"offsetgroup\": \"\", \"orientation\": \"v\", \"showlegend\": false, \"textposition\": \"auto\", \"type\": \"bar\", \"x\": [\"2020\", \"2025\", \"2030\", \"2035\", \"2040\", \"2045\", \"2050\", \"2055\"], \"xaxis\": \"x\", \"y\": [4811659, 5216528, 5617172, 6021824, 6411068, 6773367, 7117010, 7453472], \"yaxis\": \"y\"}],\n", | |
" {\"barmode\": \"relative\", \"height\": 400, \"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.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.0, \"#f0f921\"]], \"type\": \"contour\"}], \"contourcarpet\": [{\"colorbar\": {\"outlinewidth\": 0, \"ticks\": \"\"}, \"type\": \"contourcarpet\"}], \"heatmap\": [{\"colorbar\": {\"outlinewidth\": 0, \"ticks\": \"\"}, \"colorscale\": [[0.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.0, \"#f0f921\"]], \"type\": \"heatmap\"}], \"heatmapgl\": [{\"colorbar\": {\"outlinewidth\": 0, \"ticks\": \"\"}, \"colorscale\": [[0.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.0, \"#f0f921\"]], \"type\": \"heatmapgl\"}], \"histogram\": [{\"marker\": {\"colorbar\": {\"outlinewidth\": 0, \"ticks\": \"\"}}, \"type\": \"histogram\"}], \"histogram2d\": [{\"colorbar\": {\"outlinewidth\": 0, \"ticks\": \"\"}, \"colorscale\": [[0.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.0, \"#f0f921\"]], \"type\": \"histogram2d\"}], \"histogram2dcontour\": [{\"colorbar\": {\"outlinewidth\": 0, \"ticks\": \"\"}, \"colorscale\": [[0.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.0, \"#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.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.0, \"#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.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.0, \"#f0f921\"]], \"sequentialminus\": [[0.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.0, \"#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}}}, \"title\": {\"text\": \"Population by simulation year\"}, \"width\": 600, \"xaxis\": {\"anchor\": \"y\", \"domain\": [0.0, 1.0], \"title\": {\"text\": \"Simulation year\"}}, \"yaxis\": {\"anchor\": \"x\", \"domain\": [0.0, 1.0], \"title\": {\"text\": \"Population\"}}},\n", | |
" {\"responsive\": true}\n", | |
" ).then(function(){\n", | |
" \n", | |
"var gd = document.getElementById('0bea95f8-eeca-4da1-995c-d0a38896972e');\n", | |
"var x = new MutationObserver(function (mutations, observer) {{\n", | |
" var display = window.getComputedStyle(gd).display;\n", | |
" if (!display || display === 'none') {{\n", | |
" console.log([gd, 'removed!']);\n", | |
" Plotly.purge(gd);\n", | |
" observer.disconnect();\n", | |
" }}\n", | |
"}});\n", | |
"\n", | |
"// Listen for the removal of the full notebook cells\n", | |
"var notebookContainer = gd.closest('#notebook-container');\n", | |
"if (notebookContainer) {{\n", | |
" x.observe(notebookContainer, {childList: true});\n", | |
"}}\n", | |
"\n", | |
"// Listen for the clearing of the current output cell\n", | |
"var outputEl = gd.closest('.output');\n", | |
"if (outputEl) {{\n", | |
" x.observe(outputEl, {childList: true});\n", | |
"}}\n", | |
"\n", | |
" })\n", | |
" };\n", | |
" });\n", | |
" </script>\n", | |
" </div>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"fig = px.bar(x=years, y=pop,\n", | |
" labels={'x': 'Simulation year', 'y': 'Population'},\n", | |
" width=600,\n", | |
" height=400)\n", | |
"\n", | |
"fig.update_layout(title_text='Population by simulation year')\n", | |
"\n", | |
"fig.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Plotly export\n", | |
"\n", | |
"Here's some information about exporting the figure to js/json/html:\n", | |
"\n", | |
"https://plot.ly/python/interactive-html-export/ \n", | |
"https://plot.ly/python/renderers/" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Altair/Vega\n", | |
"\n", | |
"https://altair-viz.github.io \n", | |
"https://vega.github.io/vega-lite/" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import altair as alt" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"\n", | |
"<div id=\"altair-viz-4843319a9c3e48aa89393435b32b4133\"></div>\n", | |
"<script type=\"text/javascript\">\n", | |
" (function(spec, embedOpt){\n", | |
" const outputDiv = document.getElementById(\"altair-viz-4843319a9c3e48aa89393435b32b4133\");\n", | |
" const paths = {\n", | |
" \"vega\": \"https://cdn.jsdelivr.net/npm//vega@5?noext\",\n", | |
" \"vega-lib\": \"https://cdn.jsdelivr.net/npm//vega-lib?noext\",\n", | |
" \"vega-lite\": \"https://cdn.jsdelivr.net/npm//vega-lite@4.0.2?noext\",\n", | |
" \"vega-embed\": \"https://cdn.jsdelivr.net/npm//vega-embed@6?noext\",\n", | |
" };\n", | |
"\n", | |
" function loadScript(lib) {\n", | |
" return new Promise(function(resolve, reject) {\n", | |
" var s = document.createElement('script');\n", | |
" s.src = paths[lib];\n", | |
" s.async = true;\n", | |
" s.onload = () => resolve(paths[lib]);\n", | |
" s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n", | |
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n", | |
" });\n", | |
" }\n", | |
"\n", | |
" function showError(err) {\n", | |
" outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n", | |
" throw err;\n", | |
" }\n", | |
"\n", | |
" function displayChart(vegaEmbed) {\n", | |
" vegaEmbed(outputDiv, spec, embedOpt)\n", | |
" .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n", | |
" }\n", | |
"\n", | |
" if(typeof define === \"function\" && define.amd) {\n", | |
" requirejs.config({paths});\n", | |
" require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n", | |
" } else if (typeof vegaEmbed === \"function\") {\n", | |
" displayChart(vegaEmbed);\n", | |
" } else {\n", | |
" loadScript(\"vega\")\n", | |
" .then(() => loadScript(\"vega-lite\"))\n", | |
" .then(() => loadScript(\"vega-embed\"))\n", | |
" .catch(showError)\n", | |
" .then(() => displayChart(vegaEmbed));\n", | |
" }\n", | |
" })({\"config\": {\"view\": {\"continuousWidth\": 400, \"continuousHeight\": 300}}, \"data\": {\"name\": \"data-ea421dade1107025078671b0d4a5bf73\"}, \"mark\": \"bar\", \"encoding\": {\"x\": {\"type\": \"nominal\", \"axis\": {\"title\": \"Simulation year\"}, \"field\": \"years\"}, \"y\": {\"type\": \"quantitative\", \"axis\": {\"title\": \"Population\"}, \"field\": \"pop\"}}, \"title\": \"Population by simulation year\", \"width\": {\"step\": 50}, \"$schema\": \"https://vega.github.io/schema/vega-lite/v4.0.2.json\", \"datasets\": {\"data-ea421dade1107025078671b0d4a5bf73\": [{\"years\": \"2020\", \"pop\": 4811659}, {\"years\": \"2025\", \"pop\": 5216528}, {\"years\": \"2030\", \"pop\": 5617172}, {\"years\": \"2035\", \"pop\": 6021824}, {\"years\": \"2040\", \"pop\": 6411068}, {\"years\": \"2045\", \"pop\": 6773367}, {\"years\": \"2050\", \"pop\": 7117010}, {\"years\": \"2055\", \"pop\": 7453472}]}}, {\"mode\": \"vega-lite\"});\n", | |
"</script>" | |
], | |
"text/plain": [ | |
"alt.Chart(...)" | |
] | |
}, | |
"execution_count": 19, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data = pd.DataFrame({'years': years, 'pop': pop})\n", | |
"\n", | |
"alt.Chart(data).mark_bar().encode(\n", | |
" x=alt.X('years', axis=alt.Axis(title='Simulation year')),\n", | |
" y=alt.Y('pop', axis=alt.Axis(title='Population'))\n", | |
").properties(\n", | |
" width=alt.Step(50),\n", | |
" title='Population by simulation year'\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Altair/Vega export\n", | |
"\n", | |
"https://altair-viz.github.io/user_guide/saving_charts.html" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment