{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Export statischer Bilder\n", "\n", "Bokeh unterstützt den Export von PNG und SVG." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PNG-Export\n", "\n", "Bokeh unterstützt das Exportieren eines Plots oder Layouts in das PNG-Bildformat mit der Funktion `export_png`. Diese Funktion enthält ein zu exportierendes Bokeh-Objekt und einen Dateinamen, in den die PNG-Ausgabe geschrieben werden soll. Häufig handelt es sich bei dem an `export_png` übergebenen Bokeh-Objekt um eine einzelne Darstellung, dies muss jedoch nicht sein. Wenn ein Layout exportiert wird, wird das gesamte Layout in einem PNG-Bild gespeichert.\n", "\n", "Die PNG-Exportfunktion erfordert die Installation einiger zusätzlicher Abhängigkeiten, z.B.:\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", "Nach der Installation von Selenium müsst ihr das [geckodriver](https://github.com/mozilla/geckodriver/releases)-Binary herunterladen und installieren. Stellt sicher, dass `geckodriver` in `PATH` verfügbar ist. Weitere Informationen findet ihr in der [geckodriver-Dokumentation](https://firefox-source-docs.mozilla.org/testing/geckodriver/Usage.html). Schließlich muss auch noch Firefox auf eurem System verfügbar sein." ] }, { "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 kann auch SVG-Ausgaben im Browser generieren, anstatt HTML-`canvas`-Elemente zu rendern. Dies wird durch Setzen von `output_backend='svg'` für eine `figure` erreicht. Anschließend kann das SVG entweder mit `output_file` in HTML-Dateien oder in mit `components` erstellten Inhalten eingebettet werden. Alternativ können mit `export_svgs` auch `.svg`-Dateien erstellt werden. Beachten Sie, dass für jedes HTML-`canvas`-Element eine SVG-Datei erstellt wird; es ist jedoch nicht möglich, vollständige Layouts oder Widgets in einer SVG-Datei zu erstellen." ] }, { "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 }