Skip to content

Instantly share code, notes, and snippets.

@xerial
Last active May 3, 2018 04:51
Show Gist options
  • Save xerial/ddaaa8d53c4515c8a3cd4239f2c8e691 to your computer and use it in GitHub Desktop.
Save xerial/ddaaa8d53c4515c8a3cd4239f2c8e691 to your computer and use it in GitHub Desktop.
Scala Jupyter Notebook Plot Example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[32mimport \u001b[39m\u001b[36m$ivy.$ \n",
"\u001b[39m\n",
"\u001b[32mimport \u001b[39m\u001b[36mvegas._\u001b[39m"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import $ivy.`org.vegas-viz::vegas:0.3.11`\n",
"import vegas._"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <iframe id=\"frame-vegas-e6551ad3-d0b4-4d51-aa93-ebc7e5e9c3a9\" sandbox=\"allow-scripts allow-same-origin\" style=\"border: none; width: 100%\" srcdoc=\"&lt;html&gt;\n",
" &lt;head&gt;\n",
" &lt;script src=&quot;https://cdn.jsdelivr.net/webjars/org.webjars.bower/d3/3.5.17/d3.min.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;\n",
"&lt;script src=&quot;https://cdn.jsdelivr.net/webjars/org.webjars.bower/vega/2.6.3/vega.min.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;\n",
"&lt;script src=&quot;https://cdn.jsdelivr.net/webjars/org.webjars.bower/vega-lite/1.2.0/vega-lite.min.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;\n",
"&lt;script src=&quot;https://vega.github.io/vega-editor/vendor/vega-embed.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;\n",
" &lt;/head&gt;\n",
" &lt;body&gt;\n",
" &lt;div id='vegas-e6551ad3-d0b4-4d51-aa93-ebc7e5e9c3a9'&gt;&lt;/div&gt;\n",
" &lt;script&gt;\n",
" var embedSpec = {\n",
" mode: &quot;vega-lite&quot;,\n",
" spec: {\n",
" &quot;mark&quot; : &quot;bar&quot;,\n",
" &quot;encoding&quot; : {\n",
" &quot;x&quot; : {\n",
" &quot;field&quot; : &quot;country&quot;,\n",
" &quot;type&quot; : &quot;nominal&quot;\n",
" },\n",
" &quot;y&quot; : {\n",
" &quot;field&quot; : &quot;population&quot;,\n",
" &quot;type&quot; : &quot;quantitative&quot;\n",
" }\n",
" },\n",
" &quot;description&quot; : &quot;Country Pop&quot;,\n",
" &quot;data&quot; : {\n",
" &quot;values&quot; : [\n",
" {\n",
" &quot;country&quot; : &quot;USA&quot;,\n",
" &quot;population&quot; : 314\n",
" },\n",
" {\n",
" &quot;country&quot; : &quot;UK&quot;,\n",
" &quot;population&quot; : 64\n",
" },\n",
" {\n",
" &quot;country&quot; : &quot;DK&quot;,\n",
" &quot;population&quot; : 80\n",
" }\n",
" ]\n",
" }\n",
"}\n",
" }\n",
" vg.embed(&quot;#vegas-e6551ad3-d0b4-4d51-aa93-ebc7e5e9c3a9&quot;, embedSpec, function(error, result) {});\n",
" &lt;/script&gt;\n",
"\n",
" &lt;/body&gt;\n",
"&lt;/html&gt;\"></iframe>\n",
" <script>\n",
" (function() {\n",
" function resizeIFrame(el, k) {\n",
" var height = el.contentWindow.document.body.scrollHeight || '400'; // Fallback in case of no scroll height\n",
" el.style.height = height + 'px';\n",
" if (k <= 10) { setTimeout(function() { resizeIFrame(el, k+1) }, 1000 + (k * 250)) };\n",
" }\n",
" resizeIFrame(document.querySelector('#frame-vegas-e6551ad3-d0b4-4d51-aa93-ebc7e5e9c3a9'), 1);\n",
" })(); // IIFE\n",
" </script>\n",
" "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"\u001b[32mimport \u001b[39m\u001b[36mvegas._\n",
"\n",
"\u001b[39m"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import vegas._\n",
"\n",
"Vegas(\"Country Pop\").\n",
" withData(\n",
" Seq(\n",
" Map(\"country\" -> \"USA\", \"population\" -> 314),\n",
" Map(\"country\" -> \"UK\", \"population\" -> 64),\n",
" Map(\"country\" -> \"DK\", \"population\" -> 80)\n",
" )\n",
" ).\n",
" encodeX(\"country\", Nom).\n",
" encodeY(\"population\", Quant).\n",
" mark(Bar).\n",
" show"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Scala",
"language": "scala",
"name": "scala"
},
"language_info": {
"codemirror_mode": "text/x-scala",
"file_extension": ".scala",
"mimetype": "text/x-scala",
"name": "scala211",
"nbconvert_exporter": "script",
"pygments_lexer": "scala",
"version": "2.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment