{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Export of static images\n", "\n", "Bokeh supports the export of PNG and SVG." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PNG export\n", "\n", "Bokeh supports the export of a plot or layout to the PNG image format using the `export_png` function. This function contains a Bokeh object to be exported and a file name in which the PNG output is to be written. The bokeh object transferred to `export_png` is often a single representation, but this does not have to be the case. When a layout is exported, the entire layout is saved in a PNG image.\n", "\n", "``` bash\n", "$ spack env activate python-38\n", "$ spack install py-selenium@3.141.0%gcc@11.2.0\n", "$ py-pillow@8.0.0%gcc@11.2.0~freetype~imagequant+jpeg~jpeg2000~lcms~tiff~webp~webpmux~xcb+zlib\n", "```\n", "\n", "After installing Selenium, you need to download and install the [geckodriver](https://github.com/mozilla/geckodriver/releases) binary. Make sure that `geckodriver` is available in `PATH`. You can find more information in the [geckodriver documentation](https://firefox-source-docs.mozilla.org/testing/geckodriver/Usage.html). Finally, Firefox must also be available on your system." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from bokeh.plotting import figure\n", "from bokeh.sampledata.stocks import AAPL\n", "\n", "\n", "df = pd.DataFrame(AAPL)\n", "df[\"date\"] = pd.to_datetime(df[\"date\"])" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/Users/veit/cusy/trn/pyviz-tutorial/docs/bokeh/embedding-export/plot.png'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from bokeh.io import export_png\n", "\n", "\n", "p = figure(width=800, height=250, x_axis_type=\"datetime\")\n", "p.line(df[\"date\"], df[\"close\"], color=\"navy\", alpha=0.5)\n", "\n", "export_png(p, filename=\"plot.png\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SVG export\n", "\n", "Bokeh can also generate SVG output in the browser instead of rendering HTML `canvas` elements. This is achieved by setting `output_backend = \"svg\"` for a `figure`. The SVG can then be embedded either in HTML files with `output_file` or in content created with `components`. Alternatively, `.svg` files can also be created with `export_svgs`. Please note that an SVG file is created for each HTML `canvas` element; however, it is not possible to create complete layouts or widgets in an SVG file." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['plot.svg']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from bokeh.io import export_svgs\n", "\n", "\n", "p = figure(width=800, height=250, x_axis_type=\"datetime\", output_backend=\"svg\")\n", "p.line(df[\"date\"], df[\"close\"], color=\"navy\", alpha=0.5)\n", "\n", "export_svgs(p, filename=\"plot.svg\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "2000200020022002200420042006200620082008201020102012201200100100200200300300400400500500600600700700" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import SVG\n", "\n", "\n", "SVG(\"plot.svg\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.11 Kernel", "language": "python", "name": "python311" }, "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.11.4" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }