{ "cells": [ { "cell_type": "markdown", "id": "4c1e11e9-bbb3-4497-8af3-2e0d9029f57e", "metadata": {}, "source": [ "# Physical Metrics Parameters for `ggsave()`\n", "\n", "Parameters `dpi`, `w`, `h` and `unit` can be used to configure export to a raster image with required physical metrics. Aspect ratio must be maintained manually between `ggsize()` and `ggsave()`. Please note that using these parameters will disable the `scale` parameter." ] }, { "cell_type": "code", "execution_count": 1, "id": "3e938c92-387d-4923-ae0e-d1f540e556aa", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "6346de4f-b253-48ba-a999-cdc2795e0a3b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "eec0a05f-8b50-41de-99ef-97c8a4df86a7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(12)\n", "data = dict(\n", " cond=np.repeat(['A', 'B'], 200),\n", " rating=np.concatenate((np.random.normal(0, 1, 200), np.random.normal(1, 1.5, 200)))\n", ")\n", "\n", "p = ggplot(data, aes(x='rating', fill='cond')) + ggsize(800, 400) + \\\n", " geom_density(color='dark_green', alpha=.7) + scale_fill_brewer(type='seq') + \\\n", " theme(panel_grid_major_x='blank')\n", "p" ] }, { "cell_type": "markdown", "id": "a45790e1-3e9e-4857-9bc2-9b9ed14c3932", "metadata": {}, "source": [ "# Default" ] }, { "cell_type": "code", "execution_count": 4, "id": "057cb57e-bb4d-4cd6-9a12-2087e7b3f5b4", "metadata": {}, "outputs": [], "source": [ "_ = ggsave(p, 'plot_ggsave.png')" ] }, { "cell_type": "markdown", "id": "5f04052f-de33-435f-9319-eb87fafb3c54", "metadata": {}, "source": [ "# `ggsave()` " ] }, { "cell_type": "code", "execution_count": 5, "id": "098544d1-8d10-4fbf-b059-586ee13dfafc", "metadata": {}, "outputs": [], "source": [ "_ = ggsave(p, 'plot_ggsave_96dpi.png', w=8, h=4, unit='in', dpi=96)" ] }, { "cell_type": "code", "execution_count": 6, "id": "7765fa4f-041d-4470-8cf7-d498028f984b", "metadata": {}, "outputs": [], "source": [ "_ = ggsave(p, 'plot_ggsave_300dpi.png', w=8, h=4, unit='in', dpi=300)" ] }, { "cell_type": "markdown", "id": "995bfc04-6330-46c9-90be-15e04b4a00df", "metadata": {}, "source": [ "# `to_png()`" ] }, { "cell_type": "code", "execution_count": 7, "id": "0c213ea1-1a7c-4c49-8afc-afa2922938f0", "metadata": {}, "outputs": [], "source": [ "_ = p.to_png('plot_to_png_96dpi.png', w=8, h=4, unit='in', dpi=96)" ] }, { "cell_type": "code", "execution_count": 8, "id": "d4043838-cd54-452b-9c6a-91efe753d9f6", "metadata": {}, "outputs": [], "source": [ "_ = p.to_png('plot_to_png_300dpi.png', w=8, h=4, unit='in', dpi=300)" ] }, { "cell_type": "markdown", "id": "66816e5d-789a-4995-a361-582a90a5090b", "metadata": {}, "source": [ "# `gggrid()`" ] }, { "cell_type": "code", "execution_count": 9, "id": "df967ed5-a014-450c-a918-a38f73eca357", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g = gggrid([\n", " p,\n", " p + coord_flip()\n", "]) + ggsize(1000, 300)\n", "g" ] }, { "cell_type": "code", "execution_count": 10, "id": "be2387fb-93b6-433c-9856-faa74bfa8831", "metadata": {}, "outputs": [], "source": [ "_ = ggsave(g, 'grid_ggsave_96dpi.png', w=10, h=3, unit='in', dpi=96)" ] }, { "cell_type": "code", "execution_count": 11, "id": "8f2737d8-45b6-4a22-a52e-f21a65e512d2", "metadata": {}, "outputs": [], "source": [ "_ = ggsave(g, 'grid_ggsave_300dpi.png', w=10, h=3, unit='in', dpi=300)" ] } ], "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.8.19" } }, "nbformat": 4, "nbformat_minor": 5 }