Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export to file-like object #938

Merged
merged 20 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactoring and add demo notebook
  • Loading branch information
RYangazov committed Dec 5, 2023
commit 41afa88c0a796f51c5b61409115a41132571d3a5
165 changes: 165 additions & 0 deletions docs/f-23f/new_save_methods.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "employed-rebate",
"metadata": {},
"source": [
"# Save methods of `ggplot()` and `gggrid()`\n",
"\n",
"Use methods `to_svg()`, `to_html()`,`to_png()`,`to_pdf()` to save charts on disc or in file-like objects.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "arranged-meter",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import io\n",
"import os\n",
"from lets_plot import *\n",
"from IPython import display"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c38745bd",
"metadata": {},
"outputs": [],
"source": [
"LetsPlot.setup_html()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "remarkable-toolbox",
"metadata": {},
"outputs": [],
"source": [
"x1 = np.random.randint(10, size=100)\n",
"p1 = ggplot({'x': x1}, aes(x='x')) + geom_bar()\n",
"\n",
"n = 100\n",
"x2 = np.arange(n)\n",
"y2 = np.random.normal(size=n)\n",
"w, h = 200, 150\n",
"p2 = ggplot({'x': x2, 'y': y2}, aes(x='x', y='y')) + ggsize(w, h)"
]
},
{
"cell_type": "markdown",
"id": "b8aadbde",
"metadata": {},
"source": [
"When `path` parameter is a string, the image is written to a file with that name."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b0fdb65",
"metadata": {},
"outputs": [],
"source": [
"file = 'plot.svg'\n",
"p1.to_svg(file)\n",
"os.path.abspath(file)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "135b5a28",
"metadata": {},
"outputs": [],
"source": [
"file = 'grid.png'\n",
"gggrid([p2 + geom_point(), p2 + geom_line()]).to_png(file)\n",
"os.path.abspath(file)"
]
},
{
"cell_type": "markdown",
"id": "8c3b3b37",
"metadata": {},
"source": [
"When `path` is a file-like object, the data is written to it by calling its write() method."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "faced-integral",
"metadata": {},
"outputs": [],
"source": [
"file_like = io.BytesIO()\n",
"p1.to_svg(file_like)\n",
"display.SVG(file_like.getvalue())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0de51f34",
"metadata": {},
"outputs": [],
"source": [
"file_like = io.BytesIO()\n",
"p1.to_png(file_like, scale = 1.0)\n",
"display.Image(file_like.getvalue())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9a77dc46",
"metadata": {},
"outputs": [],
"source": [
"file_like = io.BytesIO()\n",
"gggrid([p2 + geom_point(), p2 + geom_line()]).to_svg(file_like)\n",
"display.SVG(file_like.getvalue())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "edd47309",
"metadata": {},
"outputs": [],
"source": [
"file_like = io.BytesIO()\n",
"gggrid([p2 + geom_point(), p2 + geom_line()]).to_png(file_like, scale = 1.0)\n",
"display.Image(file_like.getvalue())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading