|
53 | 53 | "alt.renderers.enable('png', webdriver='firefox')" |
54 | 54 | ] |
55 | 55 | }, |
56 | | - { |
57 | | - "cell_type": "code", |
58 | | - "execution_count": null, |
59 | | - "metadata": {}, |
60 | | - "outputs": [], |
61 | | - "source": [ |
62 | | - "from io import BytesIO\n", |
63 | | - "\n", |
64 | | - "def render_altair(chart):\n", |
65 | | - " b = BytesIO()\n", |
66 | | - " chart.save(b, scale_factor=2.0, format='png', webdriver='firefox')\n", |
67 | | - " b.seek(0)\n", |
68 | | - " return b.read()\n", |
69 | | - "\n", |
70 | | - "\n", |
71 | | - "import ast\n", |
72 | | - "\n", |
73 | | - "def exec_then_eval(code, shell):\n", |
74 | | - " block = ast.parse(code, mode='exec')\n", |
75 | | - "\n", |
76 | | - " # assumes last node is an expression\n", |
77 | | - " last = ast.Expression(block.body.pop().value)\n", |
78 | | - "\n", |
79 | | - " _globals, _locals = {}, {}\n", |
80 | | - " shell.ex(compile(block, '<string>', mode='exec'))\n", |
81 | | - " return shell.ev(compile(last, '<string>', mode='eval'))\n", |
82 | | - "\n", |
83 | | - "@magics_class\n", |
84 | | - "class AltairMagic(Magics):\n", |
85 | | - " @cell_magic\n", |
86 | | - " def altair(self, line, cell):\n", |
87 | | - " return Image(render_altair(exec_then_eval(cell, self.shell)))\n", |
88 | | - "\n", |
89 | | - "\n", |
90 | | - "ip = get_ipython()\n", |
91 | | - "ip.register_magics(AltairMagic)" |
92 | | - ] |
93 | | - }, |
94 | 56 | { |
95 | 57 | "cell_type": "code", |
96 | 58 | "execution_count": null, |
|
111 | 73 | "theme_set(theme_grey(base_size = 24))" |
112 | 74 | ] |
113 | 75 | }, |
114 | | - { |
115 | | - "cell_type": "code", |
116 | | - "execution_count": null, |
117 | | - "metadata": {}, |
118 | | - "outputs": [], |
119 | | - "source": [] |
120 | | - }, |
121 | 76 | { |
122 | 77 | "cell_type": "markdown", |
123 | 78 | "metadata": {}, |
|
476 | 431 | }, |
477 | 432 | "outputs": [], |
478 | 433 | "source": [ |
479 | | - "%%altair\n", |
480 | 434 | "(\n", |
481 | 435 | " alt.Chart(mpg)\n", |
482 | 436 | " .mark_bar()\n", |
|
567 | 521 | }, |
568 | 522 | "outputs": [], |
569 | 523 | "source": [ |
570 | | - "%%altair \n", |
571 | 524 | "alt.Chart(mpg).mark_circle().encode(\n", |
572 | 525 | " alt.X(\n", |
573 | 526 | " \"displ\",\n", |
|
814 | 767 | }, |
815 | 768 | "outputs": [], |
816 | 769 | "source": [ |
817 | | - "%%altair \n", |
818 | 770 | "(\n", |
819 | 771 | " alt.Chart(\n", |
820 | 772 | " mpg,\n", |
|
949 | 901 | }, |
950 | 902 | "outputs": [], |
951 | 903 | "source": [ |
952 | | - "%%altair\n", |
953 | 904 | "(\n", |
954 | 905 | " alt.Chart(\n", |
955 | 906 | " mpg,\n", |
|
1069 | 1020 | }, |
1070 | 1021 | "outputs": [], |
1071 | 1022 | "source": [ |
1072 | | - "%%altair \n", |
1073 | 1023 | "alt.Chart(mpg).mark_circle().encode(\n", |
1074 | 1024 | " x=alt.X(\"displ\"),\n", |
1075 | 1025 | " y=alt.Y(\"hwy\"),\n", |
|
1191 | 1141 | }, |
1192 | 1142 | "outputs": [], |
1193 | 1143 | "source": [ |
1194 | | - "%%altair \n", |
1195 | 1144 | "(alt\n", |
1196 | 1145 | " .Chart(mpg)\n", |
1197 | 1146 | " .mark_circle()\n", |
|
1307 | 1256 | }, |
1308 | 1257 | "outputs": [], |
1309 | 1258 | "source": [ |
1310 | | - "%%altair\n", |
1311 | 1259 | "scatter = (\n", |
1312 | 1260 | " alt.Chart(\n", |
1313 | 1261 | " mpg,\n", |
|
1464 | 1412 | }, |
1465 | 1413 | "outputs": [], |
1466 | 1414 | "source": [ |
1467 | | - "%%altair\n", |
1468 | 1415 | "alt.data_transformers.disable_max_rows()\n", |
1469 | 1416 | "alt.Chart(diamonds).mark_bar().encode(\n", |
1470 | 1417 | " x=\"cut\", y=\"count(cut)\", color=\"clarity\"\n", |
|
1553 | 1500 | }, |
1554 | 1501 | "outputs": [], |
1555 | 1502 | "source": [ |
1556 | | - "%%altair\n", |
1557 | 1503 | "alt.data_transformers.disable_max_rows()\n", |
1558 | 1504 | "alt.Chart(diamonds).mark_bar().encode(\n", |
1559 | 1505 | " x=\"clarity\",\n", |
|
1728 | 1674 | }, |
1729 | 1675 | "outputs": [], |
1730 | 1676 | "source": [ |
1731 | | - "%%altair\n", |
1732 | 1677 | "alt.data_transformers.disable_max_rows()\n", |
1733 | 1678 | "alt.Chart(diamonds).transform_density(\n", |
1734 | 1679 | " \"depth\",\n", |
|
1850 | 1795 | }, |
1851 | 1796 | "outputs": [], |
1852 | 1797 | "source": [ |
1853 | | - "%%altair \n", |
1854 | 1798 | "alt.Chart(ts).mark_line().encode(\n", |
1855 | 1799 | " x=\"date\", y=\"value\"\n", |
1856 | 1800 | ")" |
|
0 commit comments