diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..0c01be5 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,13 @@ +version: 2 +updates: + - package-ecosystem: "uv" + directory: "/" + schedule: + interval: "weekly" + groups: + python-dependencies: + patterns: ["*"] + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "monthly" diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..5bf6c2d --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,78 @@ +name: Build and Deploy + +on: + push: + branches: ['**'] + pull_request: + branches: [master] + schedule: + # Weekly build-health check; deploy steps only run on push events, + # so this catches library breakage without touching production. + - cron: '0 6 * * 1' + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + env: + HAS_CF_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN != '' }} + + steps: + - name: Checkout code + uses: actions/checkout@v7 + + - name: Setup R + uses: r-lib/actions/setup-r@v2 + with: + r-version: 'release' + use-public-rspm: true + + - name: Install R packages + run: R -e "install.packages(c('ggplot2', 'mgcv'))" + + - name: Install uv + uses: astral-sh/setup-uv@v7 + with: + enable-cache: true + + - name: Install Python dependencies + run: uv sync + + - name: Install Chrome for Kaleido + run: uv run kaleido_get_chrome + + - name: Run tests + run: uv run pytest tests/ -v + + - name: Execute notebook + run: | + # Make the current R shared libraries visible to the notebook kernel. + r_library_path="$(uv run python -m rpy2.situation LD_LIBRARY_PATH)" + export LD_LIBRARY_PATH="${r_library_path}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" + uv run jupyter nbconvert --to notebook --execute Examples.ipynb --output Examples.build.ipynb + + - name: Render site + run: uv run python render.py Examples.build.ipynb + + - name: Deploy to Cloudflare Pages (Production) + if: github.ref == 'refs/heads/master' && github.event_name == 'push' && env.HAS_CF_TOKEN == 'true' + uses: cloudflare/wrangler-action@v4 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + command: pages deploy web --project-name=pythonplot --branch=main + + - name: Deploy to Cloudflare Pages (Preview) + if: github.ref != 'refs/heads/master' && env.HAS_CF_TOKEN == 'true' + uses: cloudflare/wrangler-action@v4 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + command: pages deploy web --project-name=pythonplot --branch=${{ github.head_ref || github.ref_name }} + + - name: Upload build artifacts + if: always() + uses: actions/upload-artifact@v7 + with: + name: site-build + path: web/ + retention-days: 7 diff --git a/.gitignore b/.gitignore index fdb09c4..3fcb627 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ web/index.html web/img/plots/ -Examples.*.ipynb \ No newline at end of file +Examples.*.ipynb +.setup_done +.venv/ +__pycache__/ diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 5de56ae..0000000 --- a/.travis.yml +++ /dev/null @@ -1,29 +0,0 @@ -language: python -python: - - '3.6' -notifications: - email: true -before_install: - - export PATH="./.travis:$HOME/miniconda/bin:$PATH" - - sudo apt-get -qq update - - bash .travis/install_conda.sh -install: "pip install --quiet -r requirements.txt" -dist: xenial -services: - - xvfb -script: make travis -after_script: - - echo "Rendered notebook http://nbviewer.jupyter.org/urls/s3.amazonaws.com/pythonplotbuilds/tdhopper/pythonplot.com/$TRAVIS_BUILD_NUMBER/$TRAVIS_JOB_NUMBER/Examples.$(git log -1 --pretty=format:'%h').ipynb" -before_deploy: - - npm install netlify-cli -g -deploy: - provider: script - script: ./netlify_deploy.sh - skip_cleanup: true - on: - all_branches: true -addons: - artifacts: true - apt: - packages: - firefox-geckodriver diff --git a/.travis/authenticate_plotly.py b/.travis/authenticate_plotly.py deleted file mode 100644 index 374b5c6..0000000 --- a/.travis/authenticate_plotly.py +++ /dev/null @@ -1,3 +0,0 @@ -import plotly -import os -plotly.tools.set_credentials_file(username=os.environ.get('PLOTLY_USER'), api_key=os.environ.get('PLOTLY_API_KEY')) diff --git a/.travis/install_conda.sh b/.travis/install_conda.sh deleted file mode 100644 index dcb0e81..0000000 --- a/.travis/install_conda.sh +++ /dev/null @@ -1,11 +0,0 @@ -wget --quiet https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh -bash miniconda.sh -b -p $HOME/miniconda -rm -f miniconda.sh -hash -r -$HOME/miniconda/bin/conda config --set always_yes yes --set changeps1 no -$HOME/miniconda/bin/conda update -q conda -$HOME/miniconda/bin/conda info -a -conda install --quiet pip readline r-essentials -conda install --quiet --channel r rpy2 -wget --quiet https://github.com/plotly/orca/releases/download/v1.2.1/orca-1.2.1-x86_64.AppImage -O orca.AppImage -chmod +x orca.AppImage diff --git a/.travis/invalidate_cloudfront.py b/.travis/invalidate_cloudfront.py deleted file mode 100644 index 0205286..0000000 --- a/.travis/invalidate_cloudfront.py +++ /dev/null @@ -1,19 +0,0 @@ -import boto3 -import time - -client = boto3.client('cloudfront') - -response = client.create_invalidation( - DistributionId='EL6HA8RZFI08I', - InvalidationBatch={ - 'Paths': { - 'Quantity': 2, - 'Items': [ - '/index.html', - '/img/plots/*' - ] - }, - 'CallerReference': str(time.time()) - } -) -print(response) diff --git a/.travis/orca b/.travis/orca deleted file mode 100755 index 7d54224..0000000 --- a/.travis/orca +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -xvfb-run -a ./orca.AppImage "$@" diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..d725ba4 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,139 @@ +# CLAUDE.md + +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. + +## Project Overview + +pythonplot.com is a static website that provides a visual comparison of different Python plotting libraries (pandas, matplotlib, seaborn, plotnine, lets-plot, plotly, altair) and R's ggplot2 for exploratory data analysis. It serves as a "Rosetta Stone" showing how to create the same plots across different libraries. + +## Architecture + +The site is generated from a Jupyter notebook (Examples.ipynb) that contains tagged code cells. The build process: + +1. Executes the notebook using `jupyter nbconvert` +2. Extracts tagged cells using `render.py` +3. Generates static HTML using Jinja2 templates +4. Outputs PNG images and an HTML file to the `web/` directory + +The checked-in Examples.ipynb has no outputs; the site can only be rendered from an executed copy of the notebook. + +### Key Components + +- **Examples.ipynb**: Source notebook containing plot examples for each library +- **render.py**: Core build script that: + - Extracts cells tagged with metadata (ex, name:*, package:*) + - Extracts base64 PNG images from cell outputs and saves them to web/img/plots/ + - Parses cell source code and optional markdown comments + - Renders the final HTML using Jinja2 +- **templates/t_index.html**: Jinja2 template for the website +- **web/**: Output directory for generated site; web/css/ (checked in) holds custom.css and pygments-native.css +- **INTRO.md**: Markdown content for the site introduction + +### Cell Tagging System + +Notebook cells must be tagged with metadata for the render script to process them: + +``` +ex # Marks cell as an example +name:scatter-plot # Maps to names dict in render.py +package:seaborn # Maps to packages dict in render.py +``` + +Valid plot names and packages are defined in dictionaries at the top of render.py. + +## Development Commands + +### Setup Environment + +```bash +make setup # uv sync, Chrome for Kaleido, R packages +``` + +**Prerequisites:** +- R 4.0+ installed separately (via Homebrew, apt, or CRAN) +- uv package manager (auto-installed by make targets if missing; uv provides Python) + +**Manual Setup:** +```bash +uv sync # Python dependencies (pyproject.toml + uv.lock) +uv run kaleido_get_chrome # Chrome for plotly PNG export +./setup_r.sh # R packages (ggplot2, mgcv) +``` + +### Build Site + +```bash +make qrender # Render from an already-executed Examples.ipynb (fails if outputs are stripped) +make render # Full build: execute notebook, then render (creates git-hash-stamped .ipynb) +``` + +The render process: +1. `make render` runs the notebook with `jupyter nbconvert --execute` +2. Creates a git-commit-stamped copy (Examples..ipynb) +3. Runs `python render.py` to extract cells and generate web/index.html +4. PNG images are extracted and saved to web/img/plots/ with MD5-based filenames + +### Testing + +```bash +make test # Run pytest tests +``` + +The test suite (tests/test_plots.py) validates that Examples.ipynb contains all expected plot/package tag combinations. It reads tags only, so it works on the outputs-stripped notebook. + +### Local Development + +After rendering, serve locally: + +```bash +cd web && python -m http.server +``` + +### Image Rendering + +All plots are rendered to static PNG images inside the executed notebook: +- **Plotly**: `pio.renderers.default = "png"` with Kaleido (needs Chrome; `uv run kaleido_get_chrome`) +- **Altair**: `alt.renderers.enable("png")` with vl-convert-python (set in the notebook's first cell — do not remove) +- **R/ggplot2**: rpy2 against the system R installation (`%%R` cell magic) +- **Lets-Plot** and **hvPlot/Bokeh**: an `image/png` IPython formatter registered in the notebook's first cell. Bokeh has no headless renderer, so hvPlot figures are screenshotted through Selenium driving the same Chrome for Testing build Kaleido downloads; Selenium Manager fetches the matching chromedriver on first use (needs network). + +## Adding New Plots + +1. Add the plot name to the `names` dictionary in render.py +2. Add a new cell in Examples.ipynb with code that produces a PNG output +3. Tag the cell with: `ex`, `name:`, `package:` +4. Keep code lines under ~46 characters to avoid horizontal scrolling in the UI +5. Optionally add a markdown comment in triple quotes on the first line +6. Update tests/test_plots.py to include the new plot in defined_plots +7. Run `make render` and inspect web/index.html + +## Technical Constraints + +- Plot code must generate PNG output in the notebook cell +- R code cells must start with `%%R` magic command +- Code lines should wrap to ~46 characters for proper display +- All image paths are MD5 hashes of the base64-encoded PNG data + +## Dependencies + +Python dependencies are declared in `pyproject.toml` and locked in `uv.lock` (committed). Key libraries: pandas, matplotlib, seaborn, plotnine, lets-plot, plotly (+kaleido), altair (+vl-convert-python), statsmodels, rpy2, Jinja2 with jinja2-highlight. + +R (system install) with ggplot2 and mgcv, installed by `setup_r.sh`. + +## CI/CD + +GitHub Actions (`.github/workflows/deploy.yml`) on every push: + +1. Setup R (r-lib/actions, RSPM binary packages) and install ggplot2/mgcv +2. Install uv (astral-sh/setup-uv with caching) and `uv sync` +3. `uv run kaleido_get_chrome` +4. Run pytest +5. Execute notebook and render site +6. Deploy `web/` to Cloudflare Pages (project `pythonplot`, direct upload via `cloudflare/wrangler-action`) + +Master pushes deploy with `--branch=main`, which the Cloudflare project treats as its +production branch (pythonplot.com, www.pythonplot.com). Every other branch deploys with +its own branch name, producing a preview URL. + +**Required Secrets:** `CLOUDFLARE_API_TOKEN` (needs the Cloudflare Pages: Edit +permission), `CLOUDFLARE_ACCOUNT_ID` diff --git a/Examples.ipynb b/Examples.ipynb index 79f666d..088b9ae 100644 --- a/Examples.ipynb +++ b/Examples.ipynb @@ -8,6 +8,7 @@ "source": [ "%matplotlib inline\n", "\n", + "import readline\n", "import altair as alt\n", "import pandas as pd\n", "import seaborn as sns\n", @@ -15,9 +16,8 @@ "from plotnine import *\n", "import numpy as np\n", "\n", - "from plotly import figure_factory\n", "from plotly import graph_objects\n", - "import plotly.io as pio\n", + "import plotly.express as px\n", "from IPython.core.magic import Magics, magics_class, cell_magic\n", "\n", "from IPython.display import Image\n", @@ -43,49 +43,120 @@ ") # for plotnine\n", "\n", "\n", - "fig = graph_objects.Figure(layout = dict(width=100, height=100))\n", + "import seaborn.objects as so\n", "\n", - "templated_fig = pio.to_templated(fig)\n", - "pio.templates['my_template'] = templated_fig.layout.template\n", - "pio.templates.default = 'my_template'" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from io import BytesIO\n", + "# Match the other libraries: ~750x750 px output with scaled-up text\n", + "so.Plot.config.display[\"scaling\"] = 1\n", + "so.Plot.config.theme.update(\n", + " {\n", + " \"figure.figsize\": (8, 8),\n", + " \"axes.labelsize\": size,\n", + " \"axes.titlesize\": 1.5 * size,\n", + " \"xtick.labelsize\": size,\n", + " \"ytick.labelsize\": size,\n", + " \"legend.fontsize\": size,\n", + " \"legend.title_fontsize\": size,\n", + " }\n", + ")\n", + "\n", + "import plotly.io as pio\n", + "pio.renderers.default = \"png\"\n", + "pio.renderers[\"png\"].width = 750\n", + "pio.renderers[\"png\"].height = 750\n", + "\n", + "# Render Altair charts as PNG via vl-convert\n", + "alt.renderers.enable(\"png\", scale_factor=2.0)\n", + "\n", + "import tempfile\n", + "\n", + "import lets_plot as lp\n", + "from lets_plot.export import ggsave\n", "\n", - "def render_altair(chart):\n", - " b = BytesIO()\n", - " chart.save(b, scale_factor=2.0, format='png', webdriver='firefox')\n", - " b.seek(0)\n", - " return b.read()\n", + "lp.LetsPlot.setup_html()\n", + "lp.LetsPlot.set_theme(\n", + " lp.theme(text=lp.element_text(size=16), title=lp.element_text(size=20))\n", + ")\n", + "\n", + "_LETS_PLOT_DIR = tempfile.mkdtemp(prefix=\"lets-plot-\")\n", + "\n", + "\n", + "def _lets_plot_png(plot):\n", + " \"\"\"Render a Lets-Plot spec to PNG so the cell output carries an image.\"\"\"\n", + " path = ggsave(plot + lp.ggsize(750, 750), \"plot.png\", path=_LETS_PLOT_DIR)\n", + " with open(path, \"rb\") as f:\n", + " return f.read()\n", + "\n", + "\n", + "get_ipython().display_formatter.formatters[\"image/png\"].for_type(\n", + " lp.plot.core.PlotSpec, _lets_plot_png\n", + ")\n", + "\n", + "import atexit\n", + "import io\n", + "import logging\n", "\n", + "import holoviews as hv\n", + "import hvplot.pandas # registers the .hvplot accessor on pandas objects\n", + "from bokeh.io.export import get_screenshot_as_png\n", + "from bokeh.models import Plot\n", "\n", - "import ast\n", + "hv.extension(\"bokeh\")\n", + "logging.getLogger(\"bokeh.io.export\").setLevel(logging.ERROR)\n", + "hv.plotting.bokeh.ElementPlot.toolbar = None\n", + "hv.plotting.bokeh.ElementPlot.fontscale = 1.6\n", "\n", - "def exec_then_eval(code, shell):\n", - " block = ast.parse(code, mode='exec')\n", + "_bokeh_driver = None\n", "\n", - " # assumes last node is an expression\n", - " last = ast.Expression(block.body.pop().value)\n", "\n", - " _globals, _locals = {}, {}\n", - " shell.ex(compile(block, '', mode='exec'))\n", - " return shell.ev(compile(last, '', mode='eval'))\n", + "def _bokeh_webdriver():\n", + " \"\"\"Headless Chrome for Bokeh's PNG export.\n", "\n", - "@magics_class\n", - "class AltairMagic(Magics):\n", - " @cell_magic\n", - " def altair(self, line, cell):\n", - " return Image(render_altair(exec_then_eval(cell, self.shell)))\n", + " Bokeh screenshots plots through Selenium. Reuse the Chrome for Testing\n", + " build Kaleido already downloads; Selenium Manager supplies the matching\n", + " chromedriver.\n", + " \"\"\"\n", + " global _bokeh_driver\n", + " if _bokeh_driver is None:\n", + " from selenium.webdriver.chrome.options import Options\n", + " from selenium.webdriver.chrome.webdriver import WebDriver as Chrome\n", "\n", + " options = Options()\n", + " options.add_argument(\"--headless=new\")\n", + " options.add_argument(\"--hide-scrollbars\")\n", + " options.add_argument(\"--force-device-scale-factor=1\")\n", + " options.add_argument(\"--force-color-profile=srgb\")\n", + " options.add_argument(\"--no-sandbox\")\n", + " try:\n", + " from choreographer.browsers.chromium import Chromium\n", "\n", - "ip = get_ipython()\n", - "ip.register_magics(AltairMagic)" + " options.binary_location = str(Chromium.find_browser(skip_local=False))\n", + " _bokeh_driver = Chrome(options=options)\n", + " except Exception: # fall back to whatever Chrome is on the system\n", + " options.binary_location = \"\"\n", + " _bokeh_driver = Chrome(options=options)\n", + " atexit.register(_bokeh_driver.quit)\n", + " return _bokeh_driver\n", + "\n", + "\n", + "def _hvplot_png(obj):\n", + " \"\"\"Render an hvPlot/HoloViews Bokeh figure to PNG for the cell output.\"\"\"\n", + " figure = hv.render(obj, backend=\"bokeh\")\n", + " models = figure.references()\n", + " for model in models:\n", + " if \"toolbar_location\" in model.properties():\n", + " model.toolbar_location = None\n", + " panes = [m for m in models if isinstance(m, Plot)]\n", + " if len(panes) == 1: # leave faceted grids at their per-panel size\n", + " panes[0].width, panes[0].height = 750, 750\n", + " image = get_screenshot_as_png(figure, driver=_bokeh_webdriver())\n", + " buffer = io.BytesIO()\n", + " image.save(buffer, format=\"png\")\n", + " return buffer.getvalue()\n", + "\n", + "\n", + "get_ipython().display_formatter.formatters[\"image/png\"].for_type(\n", + " hv.core.dimension.Dimensioned, _hvplot_png\n", + ")" ] }, { @@ -104,18 +175,10 @@ "outputs": [], "source": [ "%%R\n", - "install.packages(\"ggplot2\", repos='http://cran.us.r-project.org')\n", "library(ggplot2) \n", "theme_set(theme_grey(base_size = 24))" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "metadata": {}, @@ -316,6 +379,28 @@ ")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:bar-counts", + "package:lets-plot" + ] + }, + "outputs": [], + "source": [ + "\"\"\"Lets-Plot mirrors the ggplot2 grammar. It is imported as `lp` here\n", + "so its names don't collide with plotnine's.\n", + "\"\"\"\n", + "(lp.ggplot(mpg) +\n", + " lp.aes(x=\"manufacturer\") +\n", + " lp.geom_bar() +\n", + " lp.coord_flip() +\n", + " lp.ggtitle(\"Number of Cars by Make\"))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -329,17 +414,10 @@ }, "outputs": [], "source": [ - "mpgGrouped = mpg.groupby('manufacturer').size()\n", - "fig = graph_objects.Figure(layout={'title' : 'Number of Cars by Make'})\n", - "bar = graph_objects.Bar({\n", - " 'type' : 'bar',\n", - " 'x' : mpgGrouped.values.tolist(),\n", - " 'y' : mpgGrouped.index.tolist(),\n", - " 'orientation' : 'h'\n", - " \n", - " })\n", - "fig.add_trace(bar)\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.histogram(\n", + " mpg, y=\"manufacturer\", \n", + " title='Number of Cars by Make'\n", + ")" ] }, { @@ -354,7 +432,6 @@ }, "outputs": [], "source": [ - "%%altair\n", "(\n", " alt.Chart(\n", " mpg, title=\"Number of Cars by Make\"\n", @@ -366,6 +443,82 @@ ")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:bar-counts", + "package:seaborn-objects" + ] + }, + "outputs": [], + "source": [ + "\"\"\"seaborn.objects has no `coord_flip`, so the\n", + "categorical variable is mapped to `y` instead.\n", + "\"\"\"\n", + "(so.Plot(mpg, y=\"manufacturer\")\n", + " .add(so.Bar(), so.Count())\n", + " .label(title=\"Number of Cars by Make\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:bar-counts", + "package:hvplot" + ] + }, + "outputs": [], + "source": [ + "\"\"\"hvPlot exposes Bokeh through a `.hvplot`\n", + "accessor that mirrors pandas' own `.plot`.\n", + "\"\"\"\n", + "(mpg[\"manufacturer\"]\n", + " .value_counts(sort=False)\n", + " .hvplot.barh(\n", + " title=\"Number of Cars by Make\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:bar-counts", + "package:matplotlib" + ] + }, + "outputs": [], + "source": [ + "counts = mpg['manufacturer'].value_counts(\n", + " sort=False)\n", + "fig, ax = pyplot.subplots()\n", + "ax.barh(counts.index, counts.values)\n", + "ax.set_title('Number of Cars by Make');" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:bar-counts", + "package:seaborn" + ] + }, + "outputs": [], + "source": [ + "ax = sns.countplot(mpg, y='manufacturer')\n", + "ax.set_title('Number of Cars by Make');" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -441,6 +594,23 @@ " geom_histogram(binwidth=2))" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:simple-histogram", + "package:lets-plot" + ] + }, + "outputs": [], + "source": [ + "(lp.ggplot(mpg) +\n", + " lp.aes(x=\"cty\") +\n", + " lp.geom_histogram(binwidth=2))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -454,13 +624,9 @@ }, "outputs": [], "source": [ - "fig = graph_objects.Figure()\n", - "hist = graph_objects.Histogram({\n", - " 'type' : 'histogram',\n", - " 'x' : mpg['cty'],\n", - "})\n", - "fig.add_trace(hist)\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.histogram(\n", + " mpg, x=\"cty\"\n", + ")" ] }, { @@ -475,7 +641,6 @@ }, "outputs": [], "source": [ - "%%altair\n", "(\n", " alt.Chart(mpg)\n", " .mark_bar()\n", @@ -486,11 +651,57 @@ ")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:simple-histogram", + "package:seaborn-objects" + ] + }, + "outputs": [], + "source": [ + "(so.Plot(mpg, x=\"cty\")\n", + " .add(so.Bars(), so.Hist(binwidth=2)))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:simple-histogram", + "package:hvplot" + ] + }, + "outputs": [], + "source": [ + "mpg.hvplot.hist(\"cty\", bins=12)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:simple-histogram", + "package:seaborn" + ] + }, + "outputs": [], + "source": [ + "sns.histplot(mpg, x='cty', binwidth=2);" + ] + }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Scatter Plot" + "#### Box Plot of a Continuous Value by Category" ] }, { @@ -499,19 +710,16 @@ "metadata": { "tags": [ "ex", - "name:scatter-plot", + "name:box-plot", "package:ggplot" ] }, "outputs": [], "source": [ - "%%R -w 10 -h 10 -u in \n", - "ggplot(data = mpg) +\n", - " aes(x = displ, y = hwy) +\n", - " geom_point() + \n", - " ggtitle(\"Engine Displacement in Liters vs Highway MPG\") +\n", - " xlab(\"Engine Displacement in Liters\") +\n", - " ylab(\"Highway MPG\")" + "%%R -w 10 -h 10 -u in\n", + "ggplot(data=mpg) +\n", + " aes(x=class, y=hwy) +\n", + " geom_boxplot()" ] }, { @@ -520,18 +728,14 @@ "metadata": { "tags": [ "ex", - "name:scatter-plot", + "name:box-plot", "package:pandas" ] }, "outputs": [], "source": [ - "(mpg\n", - " .plot\n", - " .scatter(x='displ', y='hwy')\n", - " .set(title='Engine Displacement in Liters vs Highway MPG',\n", - " xlabel='Engine Displacement in Liters',\n", - " ylabel='Highway MPG'))" + "mpg.boxplot(column='hwy', by='class',\n", + " rot=45);" ] }, { @@ -540,18 +744,15 @@ "metadata": { "tags": [ "ex", - "name:scatter-plot", + "name:box-plot", "package:plotnine" ] }, "outputs": [], "source": [ "(ggplot(mpg) +\n", - " aes(x = \"displ\", y = \"hwy\") +\n", - " geom_point() + \n", - " ggtitle(\"Engine Displacement in Liters vs Highway MPG\") +\n", - " xlab(\"Engine Displacement in Liters\") +\n", - " ylab(\"Highway MPG\"))" + " aes(x=\"class\", y=\"hwy\") +\n", + " geom_boxplot())" ] }, { @@ -560,25 +761,15 @@ "metadata": { "tags": [ "ex", - "name:scatter-plot", - "package:altair" + "name:box-plot", + "package:lets-plot" ] }, "outputs": [], "source": [ - "%%altair \n", - "alt.Chart(mpg).mark_circle().encode(\n", - " alt.X(\n", - " \"displ\",\n", - " title=\"Engine Displacement in Liters\",\n", - " ),\n", - " alt.Y(\n", - " \"hwy\",\n", - " title=\"Highway Miles per Gallon\",\n", - " ),\n", - ").properties(\n", - " title=\"Engine Displacement in Liters\"\n", - ")" + "(lp.ggplot(mpg) +\n", + " lp.aes(x=\"class\", y=\"hwy\") +\n", + " lp.geom_boxplot())" ] }, { @@ -587,36 +778,35 @@ "metadata": { "tags": [ "ex", - "name:scatter-plot", + "name:box-plot", "package:plotly" ] }, "outputs": [], "source": [ - "fig = graph_objects.Figure(layout={\n", - " 'title' : 'Engine Displacement in Liters vs Highway MPG',\n", - " 'xaxis' : {\n", - " 'title' : 'Engine Displacement in Liters'\n", - " },\n", - " 'yaxis' : {\n", - " 'title' : 'Highway MPG'\n", - " }\n", - "})\n", - "scatter = graph_objects.Scatter({\n", - " 'type' : 'scatter',\n", - " 'mode' : 'markers',\n", - " 'x' : mpg.displ,\n", - " 'y' : mpg.hwy \n", - "})\n", - "fig.add_trace(scatter)\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.box(\n", + " mpg, x=\"class\", y=\"hwy\"\n", + ")" ] }, { - "cell_type": "markdown", - "metadata": {}, + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:box-plot", + "package:altair" + ] + }, + "outputs": [], "source": [ - "### Advanced Scatter Plotting" + "(\n", + " alt.Chart(mpg)\n", + " .mark_boxplot()\n", + " .encode(x=\"class\", y=\"hwy\")\n", + " .properties(width=400)\n", + ")" ] }, { @@ -625,17 +815,14 @@ "metadata": { "tags": [ "ex", - "name:scatter-with-regression", - "package:ggplot" + "name:box-plot", + "package:hvplot" ] }, "outputs": [], "source": [ - "%%R -w 10 -h 10 -u in \n", - "ggplot(data = mpg) +\n", - " aes(x = displ, y = hwy) +\n", - " geom_point() +\n", - " geom_smooth(method=lm)" + "mpg.hvplot.box(y=\"hwy\", by=\"class\",\n", + " rot=45)" ] }, { @@ -644,16 +831,22 @@ "metadata": { "tags": [ "ex", - "name:scatter-with-regression", - "package:plotnine" + "name:box-plot", + "package:matplotlib" ] }, "outputs": [], "source": [ - "(ggplot(mpg) +\n", - " aes(\"displ\", \"hwy\") +\n", - " geom_point() +\n", - " geom_smooth(method=\"lm\"))" + "\"\"\"`boxplot` takes a list of arrays, so the\n", + "groups are split by hand.\n", + "\"\"\"\n", + "groups = mpg.groupby('class')['hwy']\n", + "fig, ax = pyplot.subplots()\n", + "ax.boxplot([v for _, v in groups],\n", + " tick_labels=list(groups.groups))\n", + "ax.set_xlabel('class')\n", + "ax.set_ylabel('hwy')\n", + "pyplot.xticks(rotation=45);" ] }, { @@ -662,14 +855,307 @@ "metadata": { "tags": [ "ex", - "name:scatter-with-regression", + "name:box-plot", "package:seaborn" ] }, "outputs": [], "source": [ - "sns.lmplot(x=\"displ\", y=\"hwy\", \n", - " data=mpg, size=12)" + "ax = sns.boxplot(mpg, x='class', y='hwy')\n", + "ax.tick_params(axis='x', rotation=45);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Scatter Plot" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot", + "package:ggplot" + ] + }, + "outputs": [], + "source": [ + "%%R -w 10 -h 10 -u in \n", + "ggplot(data = mpg) +\n", + " aes(x = displ, y = hwy) +\n", + " geom_point() + \n", + " ggtitle(\"Engine Displacement in Liters vs Highway MPG\") +\n", + " xlab(\"Engine Displacement in Liters\") +\n", + " ylab(\"Highway MPG\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot", + "package:pandas" + ] + }, + "outputs": [], + "source": [ + "(mpg\n", + " .plot\n", + " .scatter(x='displ', y='hwy')\n", + " .set(title='Engine Displacement in Liters vs Highway MPG',\n", + " xlabel='Engine Displacement in Liters',\n", + " ylabel='Highway MPG'))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot", + "package:plotnine" + ] + }, + "outputs": [], + "source": [ + "(ggplot(mpg) +\n", + " aes(x = \"displ\", y = \"hwy\") +\n", + " geom_point() + \n", + " ggtitle(\"Engine Displacement in Liters vs Highway MPG\") +\n", + " xlab(\"Engine Displacement in Liters\") +\n", + " ylab(\"Highway MPG\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot", + "package:lets-plot" + ] + }, + "outputs": [], + "source": [ + "(lp.ggplot(mpg) +\n", + " lp.aes(x=\"displ\", y=\"hwy\") +\n", + " lp.geom_point() +\n", + " lp.ggtitle(\"Engine Displacement in Liters vs Highway MPG\") +\n", + " lp.xlab(\"Engine Displacement in Liters\") +\n", + " lp.ylab(\"Highway MPG\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot", + "package:altair" + ] + }, + "outputs": [], + "source": [ + "alt.Chart(mpg).mark_circle().encode(\n", + " alt.X(\n", + " \"displ\",\n", + " title=\"Engine Displacement in Liters\",\n", + " ),\n", + " alt.Y(\n", + " \"hwy\",\n", + " title=\"Highway Miles per Gallon\",\n", + " ),\n", + ").properties(\n", + " title=\"Engine Displacement in Liters\"\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot", + "package:plotly" + ] + }, + "outputs": [], + "source": [ + "px.scatter(\n", + " mpg, x=\"displ\", y=\"hwy\", \n", + " title='Engine Displacement in Liters vs Highway MPG',\n", + " labels=dict(\n", + " displ='Engine Displacement in Liters', \n", + " hwy='Highway MPG')\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot", + "package:seaborn-objects" + ] + }, + "outputs": [], + "source": [ + "(so.Plot(mpg, x=\"displ\", y=\"hwy\")\n", + " .add(so.Dot())\n", + " .label(\n", + " x=\"Engine Displacement in Liters\",\n", + " y=\"Highway MPG\",\n", + " title=\"Engine Displacement in Liters vs Highway MPG\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot", + "package:hvplot" + ] + }, + "outputs": [], + "source": [ + "mpg.hvplot.scatter(\n", + " x=\"displ\", y=\"hwy\",\n", + " xlabel=\"Engine Displacement in Liters\",\n", + " ylabel=\"Highway MPG\",\n", + " title=\"Engine Displacement in Liters \"\n", + " \"vs Highway MPG\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot", + "package:matplotlib" + ] + }, + "outputs": [], + "source": [ + "fig, ax = pyplot.subplots()\n", + "ax.scatter(mpg['displ'], mpg['hwy'])\n", + "ax.set_title('Engine Displacement in Liters '\n", + " 'vs Highway MPG')\n", + "ax.set_xlabel('Engine Displacement in Liters')\n", + "ax.set_ylabel('Highway MPG');" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot", + "package:seaborn" + ] + }, + "outputs": [], + "source": [ + "ax = sns.scatterplot(mpg, x='displ', y='hwy')\n", + "ax.set(\n", + " title='Engine Displacement in Liters '\n", + " 'vs Highway MPG',\n", + " xlabel='Engine Displacement in Liters',\n", + " ylabel='Highway MPG');" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Advanced Scatter Plotting" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-with-regression", + "package:ggplot" + ] + }, + "outputs": [], + "source": [ + "%%R -w 10 -h 10 -u in \n", + "ggplot(data = mpg) +\n", + " aes(x = displ, y = hwy) +\n", + " geom_point() +\n", + " geom_smooth(method=lm)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-with-regression", + "package:plotnine" + ] + }, + "outputs": [], + "source": [ + "(ggplot(mpg) +\n", + " aes(\"displ\", \"hwy\") +\n", + " geom_point() +\n", + " geom_smooth(method=\"lm\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-with-regression", + "package:lets-plot" + ] + }, + "outputs": [], + "source": [ + "(lp.ggplot(mpg) +\n", + " lp.aes(\"displ\", \"hwy\") +\n", + " lp.geom_point() +\n", + " lp.geom_smooth(method=\"lm\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-with-regression", + "package:seaborn" + ] + }, + "outputs": [], + "source": [ + "sns.lmplot(x=\"displ\", y=\"hwy\", \n", + " data=mpg, height=12)" ] }, { @@ -748,7 +1234,7 @@ "fig.add_trace(p2)\n", "fig.add_trace(p3)\n", "fig.add_trace(p4)\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "Image(fig.to_image(format=\"png\", width=750, height=750))" ] }, { @@ -813,7 +1299,6 @@ }, "outputs": [], "source": [ - "%%altair \n", "(\n", " alt.Chart(\n", " mpg,\n", @@ -844,9 +1329,9 @@ "outputs": [], "source": [ "\"\"\"seaborn.FacetGrid overrides the `rcParams['figure.figsize'] global parameter. \n", - "You have to set the size in the size with `size=` in `FacetGrid`\"\"\"\n", + "You have to set the size in the size with `height=` in `FacetGrid`\"\"\"\n", "(sns\n", - " .FacetGrid(mpg, hue=\"class\", size=10)\n", + " .FacetGrid(mpg, hue=\"class\", height=10)\n", " .map(pyplot.scatter, \"displ\", \"hwy\")\n", " .add_legend()\n", " .set(\n", @@ -863,17 +1348,18 @@ "tags": [ "ex", "name:scatter-plot-with-colors", - "package:plotnine" + "package:seaborn-objects" ] }, "outputs": [], "source": [ - "(ggplot(mpg) + \n", - " aes(x = \"displ\", y = \"hwy\", color = \"class\") +\n", - " geom_point() + \n", - " ggtitle(\"Engine Displacement in Liters vs Highway MPG\") +\n", - " xlab(\"Engine Displacement in Liters\") +\n", - " ylab(\"Highway MPG\"))" + "(so.Plot(mpg, x=\"displ\", y=\"hwy\",\n", + " color=\"class\")\n", + " .add(so.Dot())\n", + " .label(\n", + " x=\"Engine Displacement in Liters\",\n", + " y=\"Highway MPG\",\n", + " title=\"Engine Displacement in Liters vs Highway MPG\"))" ] }, { @@ -883,32 +1369,74 @@ "tags": [ "ex", "name:scatter-plot-with-colors", - "package:plotly" + "package:plotnine" ] }, "outputs": [], "source": [ - "traces = []\n", - "for cls in mpg[\"class\"].unique():\n", - " traces.append(\n", - " graph_objects.Scatter(\n", - " {\n", - " \"mode\": \"markers\",\n", - " \"x\": mpg.displ[mpg[\"class\"] == cls],\n", - " \"y\": mpg.hwy[mpg[\"class\"] == cls],\n", - " \"name\": cls,\n", - " }\n", - " )\n", - " )\n", - "fig = graph_objects.Figure(\n", - " layout={\n", - " \"title\": \"Engine Displacement in Liters vs Highway MPG\",\n", - " \"xaxis\": {\"title\": \"Engine Displacement in Liters\",},\n", - " \"yaxis\": {\"title\": \"Highway MPG\"},\n", - " },\n", - " data=traces,\n", - ")\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "(ggplot(mpg) + \n", + " aes(x = \"displ\", y = \"hwy\", color = \"class\") +\n", + " geom_point() + \n", + " ggtitle(\"Engine Displacement in Liters vs Highway MPG\") +\n", + " xlab(\"Engine Displacement in Liters\") +\n", + " ylab(\"Highway MPG\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-colors", + "package:lets-plot" + ] + }, + "outputs": [], + "source": [ + "(lp.ggplot(mpg) +\n", + " lp.aes(x=\"displ\", y=\"hwy\", color=\"class\") +\n", + " lp.geom_point() +\n", + " lp.ggtitle(\"Engine Displacement in Liters vs Highway MPG\") +\n", + " lp.xlab(\"Engine Displacement in Liters\") +\n", + " lp.ylab(\"Highway MPG\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-colors", + "package:plotly" + ] + }, + "outputs": [], + "source": [ + "px.scatter(\n", + " mpg, x=\"displ\", y=\"hwy\", color=\"class\", \n", + " title='Engine Displacement in Liters vs Highway MPG',\n", + " labels=dict(\n", + " displ='Engine Displacement in Liters', \n", + " hwy='Highway MPG')\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-colors", + "package:hvplot" + ] + }, + "outputs": [], + "source": [ + "mpg.hvplot.scatter(\n", + " x=\"displ\", y=\"hwy\", by=\"class\")" ] }, { @@ -948,11 +1476,10 @@ }, "outputs": [], "source": [ - "%%altair\n", "(\n", " alt.Chart(\n", " mpg,\n", - " title=\"Engine Displacement in Liters vs Highway MPG\",\n", + " title=\"City MPG vs Highway MPG\",\n", " )\n", " .mark_circle(opacity=0.3)\n", " .encode(\n", @@ -990,8 +1517,8 @@ " y='hwy', \n", " s=10*mpg['cyl'],\n", " alpha=.5))\n", - "ax.set_title('Engine Displacement in Liters vs Highway MPG')\n", - "ax.set_xlabel('Engine Displacement in Liters')\n", + "ax.set_title('City MPG vs Highway MPG')\n", + "ax.set_xlabel('City MPG')\n", "ax.set_ylabel('Highway MPG');" ] }, @@ -1012,6 +1539,23 @@ " geom_point(alpha=.5))" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-size", + "package:lets-plot" + ] + }, + "outputs": [], + "source": [ + "(lp.ggplot(mpg) +\n", + " lp.aes(x=\"cty\", y=\"hwy\", size=\"cyl\") +\n", + " lp.geom_point(alpha=.5))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -1024,29 +1568,87 @@ }, "outputs": [], "source": [ - "traces = [\n", - " graph_objects.Scatter(\n", - " {\n", - " \"mode\": \"markers\",\n", - " \"x\": mpg.cty,\n", - " \"y\": mpg.hwy,\n", - " \"marker\": {\"size\": mpg.cyl, \"color\": \"rgba(54,54,54,0.5)\"},\n", - " \"name\": cls,\n", - " }\n", - " )\n", - "]\n", - "\n", - "fig = graph_objects.Figure(\n", - " **{\n", - " \"data\": traces,\n", - " \"layout\": {\n", - " \"title\": \"Engine Displacement in Liters vs Highway MPG\",\n", - " \"xaxis\": {\"title\": \"Engine Displacement in Liters\",},\n", - " \"yaxis\": {\"title\": \"Highway MPG\"},\n", - " },\n", - " }\n", - ")\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.scatter(\n", + " mpg, x=\"cty\", y=\"hwy\", \n", + " size=\"cyl\", size_max=10,\n", + " title='City MPG vs Highway MPG',\n", + " labels=dict(cty='City MPG', hwy='Highway MPG')\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-size", + "package:seaborn-objects" + ] + }, + "outputs": [], + "source": [ + "(so.Plot(mpg, x=\"cty\", y=\"hwy\",\n", + " pointsize=\"cyl\")\n", + " .add(so.Dots(alpha=.5))\n", + " .scale(pointsize=(4, 12))\n", + " .label(x=\"City MPG\", y=\"Highway MPG\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-size", + "package:hvplot" + ] + }, + "outputs": [], + "source": [ + "mpg.hvplot.scatter(\n", + " x=\"cty\", y=\"hwy\",\n", + " s=\"cyl\", scale=4, alpha=0.5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-size", + "package:matplotlib" + ] + }, + "outputs": [], + "source": [ + "fig, ax = pyplot.subplots()\n", + "ax.scatter(mpg['cty'], mpg['hwy'],\n", + " s=10 * mpg['cyl'], alpha=.5)\n", + "ax.set_title('City MPG vs Highway MPG')\n", + "ax.set_xlabel('City MPG')\n", + "ax.set_ylabel('Highway MPG');" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-size", + "package:seaborn" + ] + }, + "outputs": [], + "source": [ + "ax = sns.scatterplot(mpg, x='cty', y='hwy',\n", + " size='cyl', alpha=.5)\n", + "ax.set(title='City MPG vs Highway MPG',\n", + " xlabel='City MPG',\n", + " ylabel='Highway MPG');" ] }, { @@ -1068,7 +1670,6 @@ }, "outputs": [], "source": [ - "%%altair \n", "alt.Chart(mpg).mark_circle().encode(\n", " x=alt.X(\"displ\"),\n", " y=alt.Y(\"hwy\"),\n", @@ -1112,12 +1713,29 @@ " col=\"class\", \n", " col_wrap=4, \n", " aspect=.5, \n", - " size=6)\n", + " height=6)\n", " .map(pyplot.scatter, \"displ\", \"hwy\", s=20)\n", " .fig.subplots_adjust(wspace=.2, hspace=.2)\n", ");" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-facet", + "package:seaborn-objects" + ] + }, + "outputs": [], + "source": [ + "(so.Plot(mpg, x=\"displ\", y=\"hwy\")\n", + " .add(so.Dot())\n", + " .facet(\"class\", wrap=4))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -1136,6 +1754,24 @@ " facet_wrap(\" ~ c\", nrow = 2))" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-facet", + "package:lets-plot" + ] + }, + "outputs": [], + "source": [ + "(lp.ggplot(mpg) +\n", + " lp.aes(x=\"displ\", y=\"hwy\") +\n", + " lp.geom_point() +\n", + " lp.facet_wrap(facets=\"class\", nrow=2))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -1148,8 +1784,57 @@ }, "outputs": [], "source": [ - "fig = figure_factory.create_facet_grid(df=mpg, x=\"displ\", y=\"cty\", facet_col=\"class\")\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.scatter(\n", + " mpg, x=\"displ\", y=\"hwy\", \n", + " facet_col=\"class\", facet_col_wrap=4\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-facet", + "package:hvplot" + ] + }, + "outputs": [], + "source": [ + "(mpg.hvplot.scatter(\n", + " x=\"displ\", y=\"hwy\", by=\"class\",\n", + " subplots=True, fontscale=0.65,\n", + " width=185, height=185)\n", + " .cols(4))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-facet", + "package:matplotlib" + ] + }, + "outputs": [], + "source": [ + "\"\"\"Matplotlib has no faceting. `subplots` makes\n", + "the grid; splitting the data, titling each panel\n", + "and hiding the leftover axes is manual.\n", + "\"\"\"\n", + "classes = sorted(mpg['class'].unique())\n", + "fig, axes = pyplot.subplots(\n", + " 2, 4, sharex=True, sharey=True)\n", + "for ax, c in zip(axes.flat, classes):\n", + " d = mpg[mpg['class'] == c]\n", + " ax.scatter(d['displ'], d['hwy'], s=20)\n", + " ax.set_title(c, fontsize=16)\n", + " ax.tick_params(labelsize=12)\n", + "for ax in axes.flat[len(classes):]:\n", + " ax.remove()" ] }, { @@ -1190,7 +1875,6 @@ }, "outputs": [], "source": [ - "%%altair \n", "(alt\n", " .Chart(mpg)\n", " .mark_circle()\n", @@ -1219,12 +1903,29 @@ " col=\"cyl\", \n", " row=\"drv\", \n", " aspect=.9, \n", - " size=4)\n", + " height=4)\n", " .map(pyplot.scatter, \"displ\", \"hwy\", s=20)\n", " .fig.subplots_adjust(wspace=.02, hspace=.02)\n", ");" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-facets", + "package:seaborn-objects" + ] + }, + "outputs": [], + "source": [ + "(so.Plot(mpg, x=\"displ\", y=\"hwy\")\n", + " .add(so.Dot())\n", + " .facet(col=\"cyl\", row=\"drv\"))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -1243,6 +1944,24 @@ " facet_grid(\"drv ~ cyl\"))" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-facets", + "package:lets-plot" + ] + }, + "outputs": [], + "source": [ + "(lp.ggplot(mpg) +\n", + " lp.aes(x=\"displ\", y=\"hwy\") +\n", + " lp.geom_point() +\n", + " lp.facet_grid(x=\"cyl\", y=\"drv\"))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -1255,14 +1974,68 @@ }, "outputs": [], "source": [ - "fig = figure_factory.create_facet_grid(\n", - " df=mpg, \n", - " x=\"displ\", \n", - " y=\"cty\", \n", - " facet_col=\"cyl\", \n", - " facet_row=\"drv\"\n", - ")\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.scatter(\n", + " mpg, x=\"displ\", y=\"hwy\", \n", + " facet_col=\"cyl\", facet_row=\"drv\",\n", + " category_orders=dict(cyl=[4,5,6,8])\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-facets", + "package:hvplot" + ] + }, + "outputs": [], + "source": [ + "\"\"\"`row` and `col` build a HoloViews `GridSpace`,\n", + "which sorts its panels by the facet values.\n", + "\"\"\"\n", + "mpg.hvplot.scatter(\n", + " x=\"displ\", y=\"hwy\",\n", + " row=\"cyl\", col=\"drv\", subplots=True,\n", + " fontscale=0.8, width=230, height=180)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:scatter-plot-with-facets", + "package:matplotlib" + ] + }, + "outputs": [], + "source": [ + "\"\"\"The `drv` by `cyl` grid is indexed by hand.\n", + "The strip labels are axis titles on the top row\n", + "and the right column.\n", + "\"\"\"\n", + "drvs = sorted(mpg['drv'].unique())\n", + "cyls = sorted(mpg['cyl'].unique())\n", + "fig, axes = pyplot.subplots(\n", + " len(drvs), len(cyls),\n", + " sharex=True, sharey=True)\n", + "for i, drv in enumerate(drvs):\n", + " for j, cyl in enumerate(cyls):\n", + " d = mpg[(mpg['drv'] == drv)\n", + " & (mpg['cyl'] == cyl)]\n", + " ax = axes[i, j]\n", + " ax.scatter(d['displ'], d['hwy'], s=20)\n", + " ax.tick_params(labelsize=12)\n", + "for j, cyl in enumerate(cyls):\n", + " axes[0, j].set_title(cyl, fontsize=16)\n", + "for i, drv in enumerate(drvs):\n", + " ax = axes[i, -1]\n", + " ax.set_ylabel(drv, fontsize=16)\n", + " ax.yaxis.set_label_position('right')" ] }, { @@ -1306,7 +2079,6 @@ }, "outputs": [], "source": [ - "%%altair\n", "scatter = (\n", " alt.Chart(\n", " mpg,\n", @@ -1364,6 +2136,26 @@ " ))" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-smooth-line-and-scatter", + "package:lets-plot" + ] + }, + "outputs": [], + "source": [ + "(lp.ggplot(mpg) +\n", + " lp.aes(x=\"displ\", y=\"hwy\") +\n", + " lp.geom_point(lp.aes(color=\"class\")) +\n", + " lp.geom_smooth(data=mpg[mpg[\"class\"] == \"subcompact\"],\n", + " se=False,\n", + " method=\"loess\"))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -1413,25 +2205,543 @@ " }\n", " }\n", "})\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "Image(fig.to_image(format=\"png\", width=750, height=750))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-smooth-line-and-scatter", + "package:matplotlib" + ] + }, + "outputs": [], + "source": [ + "\"\"\"Matplotlib has no smoother, so the loess fit\n", + "comes from statsmodels.\n", + "\"\"\"\n", + "import statsmodels.api as sm\n", + "\n", + "fig, ax = pyplot.subplots()\n", + "for c, d in mpg.groupby('class'):\n", + " ax.scatter(d['displ'], d['hwy'], label=c)\n", + "sub = mpg[mpg['class'] == 'subcompact']\n", + "fit = sm.nonparametric.lowess(sub['hwy'],\n", + " sub['displ'])\n", + "ax.plot(fit[:, 0], fit[:, 1], color='black')\n", + "ax.legend();" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-smooth-line-and-scatter", + "package:seaborn" + ] + }, + "outputs": [], + "source": [ + "\"\"\"`lowess=True` fits the loess curve. Seaborn\n", + "draws no confidence band around it.\n", + "\"\"\"\n", + "ax = sns.scatterplot(mpg, x='displ', y='hwy',\n", + " hue='class')\n", + "sns.regplot(mpg[mpg['class'] == 'subcompact'],\n", + " x='displ', y='hwy', lowess=True,\n", + " scatter=False, ax=ax);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-bar-chart", + "package:ggplot" + ] + }, + "outputs": [], + "source": [ + "%%R -w 10 -h 10 -u in\n", + "ggplot(data = diamonds) + \n", + " aes(x = cut, fill = clarity) +\n", + " geom_bar()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-bar-chart", + "package:plotnine" + ] + }, + "outputs": [], + "source": [ + "(ggplot(diamonds) + \n", + " aes(x=\"cut\", fill=\"clarity\") +\n", + " geom_bar())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-bar-chart", + "package:lets-plot" + ] + }, + "outputs": [], + "source": [ + "(lp.ggplot(diamonds) +\n", + " lp.aes(x=\"cut\", fill=\"clarity\") +\n", + " lp.geom_bar())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-bar-chart", + "package:altair" + ] + }, + "outputs": [], + "source": [ + "alt.data_transformers.disable_max_rows()\n", + "alt.Chart(diamonds).mark_bar().encode(\n", + " x=\"cut\", y=\"count(cut)\", color=\"clarity\"\n", + ").properties(width=300)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-bar-chart", + "package:pandas" + ] + }, + "outputs": [], + "source": [ + "(diamonds\n", + " .groupby(['cut', 'clarity'])\n", + " .size()\n", + " .unstack()\n", + " .plot.bar(stacked=True)\n", + ");" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-bar-chart", + "package:plotly" + ] + }, + "outputs": [], + "source": [ + "px.histogram(\n", + " diamonds, x=\"cut\", color=\"clarity\",\n", + " category_orders=dict(cut=[\n", + " \"Fair\", \"Good\", \"Very Good\", \n", + " \"Premium\", \"Ideal\"])\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-bar-chart", + "package:seaborn-objects" + ] + }, + "outputs": [], + "source": [ + "(so.Plot(diamonds, x=\"cut\", color=\"clarity\")\n", + " .add(so.Bar(), so.Count(), so.Stack()))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-bar-chart", + "package:hvplot" + ] + }, + "outputs": [], + "source": [ + "(diamonds\n", + " .groupby([\"cut\", \"clarity\"])\n", + " .size()\n", + " .unstack()\n", + " .hvplot.bar(stacked=True, rot=45,\n", + " legend=\"top_left\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-bar-chart", + "package:matplotlib" + ] + }, + "outputs": [], + "source": [ + "\"\"\"Matplotlib stacks bars by carrying the running\n", + "total of each series in `bottom`.\n", + "\"\"\"\n", + "counts = (diamonds\n", + " .groupby(['cut', 'clarity'])\n", + " .size()\n", + " .unstack())\n", + "fig, ax = pyplot.subplots()\n", + "bottom = np.zeros(len(counts))\n", + "for clarity in counts.columns:\n", + " ax.bar(counts.index, counts[clarity],\n", + " bottom=bottom, label=clarity)\n", + " bottom += counts[clarity].values\n", + "ax.legend();" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-bar-chart", + "package:seaborn" + ] + }, + "outputs": [], + "source": [ + "\"\"\"Classic seaborn has no bar-stacking function.\n", + "`histplot` counts the categories and stacks the\n", + "hue levels.\n", + "\"\"\"\n", + "sns.histplot(diamonds, x='cut',\n", + " hue='clarity',\n", + " multiple='stack', shrink=.8);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:dodged-bar-chart", + "package:ggplot" + ] + }, + "outputs": [], + "source": [ + "%%R -w 10 -h 10 -u in\n", + "ggplot(data = diamonds) + \n", + " aes(x = cut, fill = clarity) +\n", + " geom_bar(position = \"dodge\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "name:dodged-bar-chart", + "ex", + "package:altair" + ] + }, + "outputs": [], + "source": [ + "alt.data_transformers.disable_max_rows()\n", + "alt.Chart(diamonds).mark_bar().encode(\n", + " x=\"clarity\",\n", + " y=\"count(cut)\",\n", + " color=\"clarity\",\n", + " column=\"cut\",\n", + ").properties(width=100)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:dodged-bar-chart", + "package:plotnine" + ] + }, + "outputs": [], + "source": [ + "(ggplot(diamonds) + \n", + " aes(x='cut', fill='clarity') +\n", + " geom_bar(position = \"dodge\"))\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:dodged-bar-chart", + "package:lets-plot" + ] + }, + "outputs": [], + "source": [ + "(lp.ggplot(diamonds) +\n", + " lp.aes(x=\"cut\", fill=\"clarity\") +\n", + " lp.geom_bar(position=\"dodge\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:dodged-bar-chart", + "package:pandas" + ] + }, + "outputs": [], + "source": [ + "(diamonds\n", + " .groupby(['cut', 'clarity'])\n", + " .size()\n", + " .unstack()\n", + " .plot.bar()\n", + ");" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "name:dodged-bar-chart", + "package:plotly", + "ex" + ] + }, + "outputs": [], + "source": [ + "px.histogram(\n", + " diamonds, x=\"cut\", color=\"clarity\", barmode=\"group\",\n", + " category_orders=dict(cut=[\n", + " \"Fair\", \"Good\", \"Very Good\", \n", + " \"Premium\", \"Ideal\"])\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:dodged-bar-chart", + "package:seaborn-objects" + ] + }, + "outputs": [], + "source": [ + "(so.Plot(diamonds, x=\"cut\", color=\"clarity\")\n", + " .add(so.Bar(), so.Count(), so.Dodge()))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:dodged-bar-chart", + "package:hvplot" + ] + }, + "outputs": [], + "source": [ + "(diamonds\n", + " .groupby([\"cut\", \"clarity\"])\n", + " .size()\n", + " .unstack()\n", + " .hvplot.bar(stacked=False, rot=90,\n", + " fontscale=0.9))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:dodged-bar-chart", + "package:matplotlib" + ] + }, + "outputs": [], + "source": [ + "\"\"\"Dodging is manual: shift each series by its\n", + "own offset and move the ticks back to the group\n", + "centers.\n", + "\"\"\"\n", + "counts = (diamonds\n", + " .groupby(['cut', 'clarity'])\n", + " .size()\n", + " .unstack())\n", + "x = np.arange(len(counts))\n", + "width = .8 / len(counts.columns)\n", + "fig, ax = pyplot.subplots()\n", + "for i, clarity in enumerate(counts.columns):\n", + " ax.bar(x + i * width, counts[clarity],\n", + " width=width, label=clarity)\n", + "ax.set_xticks(x + .4 - width / 2)\n", + "ax.set_xticklabels(counts.index, rotation=45)\n", + "ax.legend();" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:dodged-bar-chart", + "package:seaborn" + ] + }, + "outputs": [], + "source": [ + "sns.countplot(diamonds, x='cut',\n", + " hue='clarity');" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-kde", + "package:ggplot" + ] + }, + "outputs": [], + "source": [ + "%%R -w 10 -h 10 -u in\n", + "ggplot(diamonds) +\n", + " aes(depth, fill = cut, colour = cut) +\n", + " geom_density(alpha = 0.1) +\n", + " xlim(55, 70)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-kde", + "package:pandas" + ] + }, + "outputs": [], + "source": [ + "\"\"\"I don't know whether Pandas can fill a KDE curve.\n", + "\n", + "This requires using some Matplotlib to get them to stack and to have a legend.\n", + "\"\"\"\n", + "fig, ax = pyplot.subplots()\n", + "ax.set_xlim(55, 70)\n", + "for cut in diamonds['cut'].unique():\n", + " s = diamonds[diamonds[\"cut\"] == cut]['depth']\n", + " s.plot.kde(ax=ax, label=cut)\n", + "ax.legend()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-kde", + "package:seaborn" + ] + }, + "outputs": [], + "source": "(sns\n .FacetGrid(diamonds, \n hue=\"cut\", \n height=10, \n xlim=(55, 70))\n .map(sns.kdeplot, 'depth', fill=True)\n .add_legend()\n);" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:stacked-kde", + "package:seaborn-objects" + ] + }, + "outputs": [], + "source": [ + "\"\"\"`.limit()` only clips the axis; unlike ggplot2's\n", + "`xlim()` it does not drop rows before the density is\n", + "estimated.\n", + "\"\"\"\n", + "(so.Plot(diamonds, x=\"depth\", color=\"cut\")\n", + " .add(so.Area(alpha=.1), so.KDE(gridsize=500))\n", + " .limit(x=(55, 70)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { + "scrolled": false, "tags": [ "ex", - "name:stacked-bar-chart", - "package:ggplot" + "name:stacked-kde", + "package:plotnine" ] }, "outputs": [], "source": [ - "%%R -w 10 -h 10 -u in\n", - "ggplot(data = diamonds) + \n", - " aes(x = cut, fill = clarity) +\n", - " geom_bar()" + "\"\"\"`+ xlim(55, 70)` results in an error.\"\"\"\n", + "(ggplot(diamonds) +\n", + " aes('depth', fill='cut', color='cut') +\n", + " geom_density(alpha=0.1))" ] }, { @@ -1440,15 +2750,16 @@ "metadata": { "tags": [ "ex", - "name:stacked-bar-chart", - "package:plotnine" + "name:stacked-kde", + "package:lets-plot" ] }, "outputs": [], "source": [ - "(ggplot(diamonds) + \n", - " aes(x=\"cut\", fill=\"clarity\") +\n", - " geom_bar())" + "(lp.ggplot(diamonds) +\n", + " lp.aes(\"depth\", fill=\"cut\", color=\"cut\") +\n", + " lp.geom_density(alpha=0.1) +\n", + " lp.xlim(55, 70))" ] }, { @@ -1457,17 +2768,24 @@ "metadata": { "tags": [ "ex", - "name:stacked-bar-chart", + "name:stacked-kde", "package:altair" ] }, "outputs": [], "source": [ - "%%altair\n", "alt.data_transformers.disable_max_rows()\n", - "alt.Chart(diamonds).mark_bar().encode(\n", - " x=\"cut\", y=\"count(cut)\", color=\"clarity\"\n", - ").properties(width=300)" + "alt.Chart(diamonds).transform_density(\n", + " \"depth\",\n", + " as_=[\"depth\", \"density\"],\n", + " groupby=[\"cut\"],\n", + " extent=[55, 70],\n", + ").mark_area(fillOpacity=0.3,).encode(\n", + " x=\"depth\",\n", + " y=\"density:Q\",\n", + " color=\"cut\",\n", + " stroke=\"cut\",\n", + ")" ] }, { @@ -1476,18 +2794,26 @@ "metadata": { "tags": [ "ex", - "name:stacked-bar-chart", - "package:pandas" + "name:stacked-kde", + "package:plotly" ] }, "outputs": [], "source": [ - "(diamonds\n", - " .groupby(['cut', 'clarity'])\n", - " .size()\n", - " .unstack()\n", - " .plot.bar(stacked=True)\n", - ");" + "from scipy.stats import gaussian_kde\n", + "\n", + "fig = graph_objects.Figure()\n", + "for cut in diamonds[\"cut\"].unique():\n", + " depth = diamonds.loc[\n", + " diamonds[\"cut\"] == cut, \"depth\"]\n", + " grid = np.linspace(\n", + " depth.min(), depth.max(),\n", + " 500, endpoint=False)\n", + " fig.add_trace(graph_objects.Scatter(\n", + " x=grid, y=gaussian_kde(depth)(grid),\n", + " mode=\"lines\", fill=\"tozeroy\",\n", + " name=cut))\n", + "Image(fig.to_image(format=\"png\", width=750, height=750))" ] }, { @@ -1496,30 +2822,19 @@ "metadata": { "tags": [ "ex", - "name:stacked-bar-chart", - "package:plotly" + "name:stacked-kde", + "package:hvplot" ] }, "outputs": [], "source": [ - "traces = []\n", - "newDiamond = diamonds.groupby(['cut','clarity']).size().unstack()\n", - "for c in newDiamond.columns:\n", - " traces.append(graph_objects.Bar({\n", - " 'x' : newDiamond.index,\n", - " 'y' : newDiamond[c],\n", - " 'name' : c\n", - " }))\n", - "fig = graph_objects.Figure(**{\n", - " 'data' : traces,\n", - " 'layout' : {\n", - " 'barmode' : 'stack',\n", - " 'xaxis' : {\n", - " 'title' : 'cut'\n", - " }, \n", - " }\n", - "})\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "\"\"\"`xlim` only clips the axis. Unlike ggplot2's\n", + "`xlim()` it doesn't drop rows before the\n", + "densities are estimated.\n", + "\"\"\"\n", + "diamonds.hvplot.kde(\n", + " y=\"depth\", by=\"cut\",\n", + " alpha=0.1, xlim=(55, 70))" ] }, { @@ -1528,16 +2843,26 @@ "metadata": { "tags": [ "ex", - "name:dodged-bar-chart", - "package:ggplot" + "name:stacked-kde", + "package:matplotlib" ] }, "outputs": [], "source": [ - "%%R -w 10 -h 10 -u in\n", - "ggplot(data = diamonds) + \n", - " aes(x = cut, fill = clarity) +\n", - " geom_bar(position = \"dodge\")" + "\"\"\"Matplotlib has no density estimator, so the\n", + "KDE comes from scipy. `set_xlim` clips the axis;\n", + "ggplot2's `xlim()` drops rows first.\n", + "\"\"\"\n", + "from scipy.stats import gaussian_kde\n", + "\n", + "grid = np.linspace(55, 70, 200)\n", + "fig, ax = pyplot.subplots()\n", + "for cut, d in diamonds.groupby('cut')['depth']:\n", + " density = gaussian_kde(d)(grid)\n", + " ax.fill_between(grid, density, alpha=.1)\n", + " ax.plot(grid, density, label=cut)\n", + "ax.set_xlim(55, 70)\n", + "ax.legend();" ] }, { @@ -1545,21 +2870,15 @@ "execution_count": null, "metadata": { "tags": [ - "name:dodged-bar-chart", "ex", - "package:altair" + "name:timeseries", + "package:ggplot" ] }, "outputs": [], "source": [ - "%%altair\n", - "alt.data_transformers.disable_max_rows()\n", - "alt.Chart(diamonds).mark_bar().encode(\n", - " x=\"clarity\",\n", - " y=\"count(cut)\",\n", - " color=\"clarity\",\n", - " column=\"cut\",\n", - ").properties(width=100)" + "%%R -i ts -w 10 -h 10 -u in \n", + "ggplot(ts) + aes(date, value) + geom_line()" ] }, { @@ -1568,15 +2887,15 @@ "metadata": { "tags": [ "ex", - "name:dodged-bar-chart", + "name:timeseries", "package:plotnine" ] }, "outputs": [], "source": [ - "(ggplot(diamonds) + \n", - " aes(x='cut', fill='clarity') +\n", - " geom_bar(position = \"dodge\"))\n" + "(ggplot(ts) \n", + " + aes(\"date\", \"value\") \n", + " + geom_line())" ] }, { @@ -1585,18 +2904,15 @@ "metadata": { "tags": [ "ex", - "name:dodged-bar-chart", - "package:pandas" + "name:timeseries", + "package:lets-plot" ] }, "outputs": [], "source": [ - "(diamonds\n", - " .groupby(['cut', 'clarity'])\n", - " .size()\n", - " .unstack()\n", - " .plot.bar()\n", - ");" + "(lp.ggplot(ts)\n", + " + lp.aes(\"date\", \"value\")\n", + " + lp.geom_line())" ] }, { @@ -1604,31 +2920,14 @@ "execution_count": null, "metadata": { "tags": [ - "name:dodged-bar-chart", - "package:plotly", - "ex" + "ex", + "name:timeseries", + "package:pandas" ] }, "outputs": [], "source": [ - "traces = []\n", - "newDiamond = diamonds.groupby(['cut','clarity']).size().unstack()\n", - "for c in newDiamond.columns:\n", - " traces.append(graph_objects.Bar({\n", - " 'x' : newDiamond.index,\n", - " 'y' : newDiamond[c],\n", - " 'name' : c\n", - " }))\n", - "fig = graph_objects.Figure(**{\n", - " 'data' : traces,\n", - " 'layout' : {\n", - " 'barmode' : 'group',\n", - " 'xaxis' : {\n", - " 'title' : 'cut'\n", - " }, \n", - " }\n", - "})\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "ts.set_index('date')['value'].plot()" ] }, { @@ -1637,17 +2936,15 @@ "metadata": { "tags": [ "ex", - "name:stacked-kde", - "package:ggplot" + "name:timeseries", + "package:plotly" ] }, "outputs": [], "source": [ - "%%R -w 10 -h 10 -u in\n", - "ggplot(diamonds) +\n", - " aes(depth, fill = cut, colour = cut) +\n", - " geom_density(alpha = 0.1) +\n", - " xlim(55, 70)" + "px.line(\n", + " ts, x=\"date\", y=\"value\"\n", + ")" ] }, { @@ -1656,22 +2953,15 @@ "metadata": { "tags": [ "ex", - "name:stacked-kde", - "package:pandas" + "name:timeseries", + "package:altair" ] }, "outputs": [], "source": [ - "\"\"\"I don't know whether Pandas can fill a KDE curve.\n", - "\n", - "This requires using some Matplotlib to get them to stack and to have a legend.\n", - "\"\"\"\n", - "fig, ax = pyplot.subplots()\n", - "ax.set_xlim(55, 70)\n", - "for cut in diamonds['cut'].unique():\n", - " s = diamonds[diamonds[\"cut\"] == cut]['depth']\n", - " s.plot.kde(ax=ax, label=cut)\n", - "ax.legend()" + "alt.Chart(ts).mark_line().encode(\n", + " x=\"date\", y=\"value\"\n", + ")" ] }, { @@ -1680,39 +2970,29 @@ "metadata": { "tags": [ "ex", - "name:stacked-kde", - "package:seaborn" + "name:timeseries", + "package:seaborn-objects" ] }, "outputs": [], "source": [ - "(sns\n", - " .FacetGrid(diamonds, \n", - " hue=\"cut\", \n", - " size=10, \n", - " xlim=(55, 70))\n", - " .map(sns.kdeplot, 'depth', shade=True)\n", - " .add_legend()\n", - ");" + "(so.Plot(ts, x=\"date\", y=\"value\")\n", + " .add(so.Line()))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "scrolled": false, "tags": [ "ex", - "name:stacked-kde", - "package:plotnine" + "name:timeseries", + "package:hvplot" ] }, "outputs": [], "source": [ - "\"\"\"`+ xlim(55, 70)` results in an error.\"\"\"\n", - "(ggplot(diamonds) +\n", - " aes('depth', fill='cut', color='cut') +\n", - " geom_density(alpha=0.1))" + "ts.hvplot.line(x=\"date\", y=\"value\")" ] }, { @@ -1721,25 +3001,14 @@ "metadata": { "tags": [ "ex", - "name:stacked-kde", - "package:altair" + "name:timeseries", + "package:matplotlib" ] }, "outputs": [], "source": [ - "%%altair\n", - "alt.data_transformers.disable_max_rows()\n", - "alt.Chart(diamonds).transform_density(\n", - " \"depth\",\n", - " as_=[\"depth\", \"density\"],\n", - " groupby=[\"cut\"],\n", - " extent=[55, 70],\n", - ").mark_area(fillOpacity=0.3,).encode(\n", - " x=\"depth\",\n", - " y=\"density:Q\",\n", - " color=\"cut\",\n", - " stroke=\"cut\",\n", - ")" + "fig, ax = pyplot.subplots()\n", + "ax.plot(ts['date'], ts['value']);" ] }, { @@ -1748,23 +3017,20 @@ "metadata": { "tags": [ "ex", - "name:stacked-kde", - "package:plotly" + "name:timeseries", + "package:seaborn" ] }, "outputs": [], "source": [ - "fig = figure_factory.create_distplot(\n", - " [diamonds[\"depth\"][diamonds[\"cut\"] == c].values \n", - " for c in diamonds.cut.unique()\n", - " ],\n", - " diamonds.cut.unique(),\n", - " show_hist=False,\n", - " show_rug=False,\n", - ")\n", - "for d in fig[\"data\"]:\n", - " d.update({\"fill\": \"tozeroy\"})\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "sns.lineplot(ts, x='date', y='value');" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Heatmap of Counts by Two Categories" ] }, { @@ -1773,14 +3039,20 @@ "metadata": { "tags": [ "ex", - "name:timeseries", + "name:heatmap", "package:ggplot" ] }, "outputs": [], "source": [ - "%%R -i ts -w 10 -h 10 -u in \n", - "ggplot(ts) + aes(date, value) + geom_line()" + "%%R -w 10 -h 10 -u in\n", + "counts <- as.data.frame(\n", + " table(cut=diamonds$cut,\n", + " clarity=diamonds$clarity),\n", + " responseName=\"count\")\n", + "ggplot(data=counts) +\n", + " aes(x=cut, y=clarity, fill=count) +\n", + " geom_tile()" ] }, { @@ -1789,15 +3061,19 @@ "metadata": { "tags": [ "ex", - "name:timeseries", + "name:heatmap", "package:plotnine" ] }, "outputs": [], "source": [ - "(ggplot(ts) \n", - " + aes(\"date\", \"value\") \n", - " + geom_line())" + "counts = (diamonds\n", + " .groupby([\"cut\", \"clarity\"])\n", + " .size()\n", + " .reset_index(name=\"count\"))\n", + "(ggplot(counts) +\n", + " aes(x=\"cut\", y=\"clarity\", fill=\"count\") +\n", + " geom_tile())" ] }, { @@ -1806,13 +3082,20 @@ "metadata": { "tags": [ "ex", - "name:timeseries", - "package:pandas" + "name:heatmap", + "package:lets-plot" ] }, "outputs": [], "source": [ - "ts.set_index('date')['value'].plot()" + "counts = (diamonds\n", + " .groupby([\"cut\", \"clarity\"])\n", + " .size()\n", + " .reset_index(name=\"count\"))\n", + "(lp.ggplot(counts) +\n", + " lp.aes(x=\"cut\", y=\"clarity\",\n", + " fill=\"count\") +\n", + " lp.geom_tile())" ] }, { @@ -1821,20 +3104,15 @@ "metadata": { "tags": [ "ex", - "name:timeseries", + "name:heatmap", "package:plotly" ] }, "outputs": [], "source": [ - "fig = graph_objects.Figure(layout={'xaxis' : { 'title' : 'date'}})\n", - "scatter = graph_objects.Scatter({\n", - " 'mode' :'lines',\n", - " 'x' : ts.date,\n", - " 'y' : ts.value\n", - "})\n", - "fig.add_trace(scatter)\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.density_heatmap(\n", + " diamonds, x=\"cut\", y=\"clarity\"\n", + ")" ] }, { @@ -1843,17 +3121,102 @@ "metadata": { "tags": [ "ex", - "name:timeseries", + "name:heatmap", "package:altair" ] }, "outputs": [], "source": [ - "%%altair \n", - "alt.Chart(ts).mark_line().encode(\n", - " x=\"date\", y=\"value\"\n", + "alt.data_transformers.disable_max_rows()\n", + "(\n", + " alt.Chart(diamonds)\n", + " .mark_rect()\n", + " .encode(\n", + " x=\"cut\", y=\"clarity\",\n", + " color=\"count()\",\n", + " )\n", + " .properties(width=400, height=400)\n", ")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:heatmap", + "package:hvplot" + ] + }, + "outputs": [], + "source": [ + "\"\"\"hvPlot needs the counts up front; `C` names\n", + "the column that colors each tile.\n", + "\"\"\"\n", + "(diamonds\n", + " .groupby([\"cut\", \"clarity\"])\n", + " .size()\n", + " .reset_index(name=\"count\")\n", + " .hvplot.heatmap(x=\"cut\", y=\"clarity\",\n", + " C=\"count\", rot=45))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:heatmap", + "package:matplotlib" + ] + }, + "outputs": [], + "source": [ + "\"\"\"Matplotlib has no heatmap function, so the\n", + "counts are pivoted and drawn with `pcolormesh`.\n", + "\"\"\"\n", + "counts = (diamonds\n", + " .groupby(['clarity', 'cut'])\n", + " .size()\n", + " .unstack())\n", + "fig, ax = pyplot.subplots()\n", + "mesh = ax.pcolormesh(counts.values)\n", + "ax.set_xticks(\n", + " np.arange(len(counts.columns)) + .5,\n", + " counts.columns, rotation=45)\n", + "ax.set_yticks(\n", + " np.arange(len(counts.index)) + .5,\n", + " counts.index)\n", + "ax.set_xlabel('cut')\n", + "ax.set_ylabel('clarity')\n", + "fig.colorbar(mesh, label='count');" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "ex", + "name:heatmap", + "package:seaborn" + ] + }, + "outputs": [], + "source": [ + "\"\"\"`heatmap` colors a matrix, so the counts are\n", + "pivoted into one first.\n", + "\"\"\"\n", + "counts = (diamonds\n", + " .groupby(['clarity', 'cut'])\n", + " .size()\n", + " .unstack())\n", + "ax = sns.heatmap(\n", + " counts, cbar_kws=dict(label='count'))\n", + "ax.tick_params(axis='y', rotation=0);" + ] } ], "metadata": { @@ -1873,7 +3236,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.0" + "version": "3.7.7" } }, "nbformat": 4, diff --git a/INTRO.md b/INTRO.md index 4082c55..ae35bcd 100644 --- a/INTRO.md +++ b/INTRO.md @@ -1,77 +1,68 @@ ## Introduction -Plotting is an essential component of data analysis. As a data scientist, -I spend a significant amount of my time making simple plots to understand complex data sets (exploratory data analysis) and help others understand them (presentations). +As a data scientist, I spend much of my time making simple plots to understand complex data sets (exploratory data analysis) and help others understand them (presentations). In particular, I make a lot of bar charts (including histograms), line plots (including time series), scatter plots, and density plots from data in [Pandas data frames](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html "pandas.DataFrame documentation"). I often want to facet these on various categorical variables and layer them on a common grid. ### Python Plotting Options -Python plotting libraries are manifold. Most well known is Matplotlib. +Python has many plotting libraries. Matplotlib is the best known, and several others build on it. -"[Matplotlib](https://matplotlib.org/ "Matplotlib: Python plotting") is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms." Native Matplotlib is the cause of [frustration](https://stackoverflow.com/questions/tagged/matplotlib) to many data analysts due to the complex syntax. Much of that frustration would be alleviated if it were recognized as a library of lower level plotting primitives on which other tools can be built.[^frustration] +"[Matplotlib](https://matplotlib.org/ "Matplotlib: Visualization with Python") makes easy things easy and hard things possible." It hands you figures, axes, and drawing primitives. You assemble everything above that level yourself: faceting, stacking, density estimation, smoothing. That assembly is what sends analysts to [Stack Overflow](https://stackoverflow.com/questions/tagged/matplotlib). -[^frustration]: If you are frustrated by Matplotlib and haven't read [Effectively Using Matplotlib](http://pbpython.com/effective-matplotlib.html) by [Chris Moffitt](https://twitter.com/chris1610), go read it. +Put the Matplotlib and ggplot2 versions of the two-variable faceted scatter plot below side by side: eighteen lines of subplot bookkeeping against four lines of grammar. If Matplotlib annoys you and you haven't read [Effectively Using Matplotlib](http://pbpython.com/effective-matplotlib.html) by [Chris Moffitt](https://twitter.com/chris1610), go read it. #### Matplotlib-Based Libraries -Many excellent plotting tools are built on top of Matplotlib. +[Pandas plotting](https://pandas.pydata.org/docs/user_guide/visualization.html "pandas user guide: Chart Visualization") provides "the basics ... to easily create decent looking plots" from data frames. That is about 70% of what I do day-to-day. It has no faceting, no categorical color mapping, no smoothing, and no heatmap, so six of the examples below have no pandas column. -[Pandas plots](https://pandas.pydata.org/pandas-docs/stable/visualization.html "pandas documentation") provides the "basics to easily create decent looking plots" from data frames. It provides about 70% of what I want to do day-to-day. Importantly, it lacks robust faceting capabilities. +Seaborn calls itself "[statistical data visualization](https://seaborn.pydata.org/ "seaborn: statistical data visualization")." Its classic interface is a set of named functions (`histplot`, `scatterplot`, `countplot`, `lmplot`, `kdeplot`) plus [FacetGrid](http://seaborn.pydata.org/tutorial/axis_grids.html), which I use for faceting more than anything else in the library. It covers every plot below, once you know which function to reach for. -"[plotnine](https://plotnine.readthedocs.io/en/stable/) is an implementation of a grammar of graphics in Python, it is based on ggplot2." plotnine is a recent attempt to directly translate ggplot2 to Python; despite some quirks and bugs, it works very well for a young product.[^ggpy] +Seaborn 0.12 added [seaborn.objects](https://seaborn.pydata.org/tutorial/objects_interface.html), a second interface built on the grammar of graphics. It composes a plot from marks and statistical transforms instead of dispatching to a named plotting function. The interface has no loess smoother, no regression confidence band, and neither a box-plot nor a rectangle mark, so four of the examples are missing. -"[Seaborn](https://seaborn.pydata.org/ "Seaborn: statistical data visualization") is a Python visualization library based on matplotlib. It provides a high-level interface for drawing attractive statistical graphics." Seaborn makes beautiful plots but is geared toward specific statistical plots, not general purpose plotting. It does have a powerful [faceting utility function](http://seaborn.pydata.org/tutorial/axis_grids.html) that I use regularly. - -[^ggpy]: There is also [ggpy](https://github.com/yhat/ggpy "ggplot port for python"), a ggplot2 port, from the company formerly known as yhat. It appears to be abandoned. +"[plotnine](https://plotnine.org/) is a data visualization package for Python based on the grammar of graphics." It tracks ggplot2 closely enough that most R code translates line for line, down to the `+` for layering. I reach for it when I want ggplot2 semantics without leaving Python. #### Interactive Plotting Libraries -There are several tools that can make the kinds of plots described here. At present, I have little experience with them. If anyone would like to help add examples, please [get in touch](https://github.com/tdhopper/pythonplot.com). - -"[Altair](https://altair-viz.github.io/ "Declarative Visualization in Python") is a declarative statistical visualization library for Python, based on [Vega-Lite](https://vega.github.io/vega-lite/ "Vega-Lite: A High-Level Visualization Grammar for Interactive Graphics")." According to Jake Vanderplas, "Declarative visualization lets you think about data and relationships, rather than incidental details."[^jake] - -Fundamentally, Altair renders JSON-descriptions of plots that are rendered by Vega-Lite; this JSON could be rendered by other backends as well, and I'm told a Matplotlib backend is under development. - -Altair is new on the scene and offers a lot of promise. I hope to add Altair examples here now that 2.0 is released. +These libraries draw in the browser. The examples here are static PNGs, so their tooltips, panning, and linked selection are gone. -[^jake]: See [here](https://speakerdeck.com/jakevdp/visualization-in-python-with-altair). +"[Vega-Altair](https://altair-viz.github.io/ "Vega-Altair: Declarative Visualization in Python") is a declarative visualization library for Python," built on [Vega-Lite](https://vega.github.io/vega-lite/ "Vega-Lite: A High-Level Visualization Grammar for Interactive Graphics"). According to [Jake Vanderplas](https://speakerdeck.com/jakevdp/visualization-in-python-with-altair), "Declarative visualization lets you think about data and relationships, rather than incidental details." You describe the encoding and Altair chooses the marks, scales, and legend. -"[Bokeh](http://bokeh.pydata.org/en/latest/ "Python interactive visualization library") is a Python interactive visualization library that targets modern web browsers for presentation." +"[plotly](https://plotly.com/python/ "Plotly Open Source Graphing Library for Python")'s Python graphing library makes interactive, publication-quality graphs." The examples here use [Plotly Express](https://plotly.com/python/plotly-express/), which the project calls "the recommended starting point for creating most common figures." Express covers most of these plots in one call; the regression and smoothing examples fall back to `graph_objects` and statsmodels. -"[bqplot](https://github.com/bloomberg/bqplot) is a Grammar of Graphics-based interactive plotting framework for the Jupyter notebook." +JetBrains writes [Lets-Plot](https://lets-plot.org/ "Lets-Plot: multiplatform plotting library built on the principles of the Grammar of Graphics"), which it calls "a faithful port of R's ggplot2 to Python and Kotlin." The claim holds up: most of the examples below are the ggplot2 column with `lp.` prefixes. Like Altair, it renders to HTML in the notebook. -"[plotly](https://plot.ly/ "Plotly - Make charts and dashboards online")'s Python graphing library makes interactive, publication-quality graphs online. Examples of how to make line plots, scatter plots, area charts, bar charts, error bars, box plots, histograms, heatmaps, subplots, multiple-axes, polar charts, and bubble charts." +"[Bokeh](https://docs.bokeh.org/en/latest/ "Bokeh documentation") is a Python library for creating interactive visualizations for modern web browsers." The Bokeh examples below go through [hvPlot](https://hvplot.holoviz.org/), which adds an `.hvplot` accessor to data frames. The accessor echoes the pandas `.plot` API, so most of these plots are one call plus a few keyword arguments. hvPlot has no regression line or loess smoother, so it is absent from those two examples. -### The Python Plotting Landscape +### Further Reading -If you're interested in the breadth of plotting tools available for Python, I commend Jake Vanderplas's Pycon 2017 talk called the [The Python Visualization Landscape](https://www.youtube.com/watch?v=FytuB8nFHPQ). Similarly, the blogpost [A Dramatic Tour through Python's Data Visualization Landscape (including ggplot and Altair)](https://dsaber.com/2016/10/02/a-dramatic-tour-through-pythons-data-visualization-landscape-including-ggplot-and-altair/) by Dan Saber is worth your time. +Jake Vanderplas's PyCon 2017 talk [The Python Visualization Landscape](https://www.youtube.com/watch?v=FytuB8nFHPQ) still explains how these libraries relate to one another, as does Dan Saber's [A Dramatic Tour through Python's Data Visualization Landscape (including ggplot and Altair)](https://dsaber.com/2016/10/02/a-dramatic-tour-through-pythons-data-visualization-landscape-including-ggplot-and-altair/), though both predate several of the libraries here. ### Hearty Thank You -Much Python plotting development is done by open source developers who have an (almost) thankless task. I am extremely grateful for the countless hours of many who have helped me do my job. Please keep it up! +Open source developers do most Python plotting development, an (almost) thankless job. I am grateful for the hours they have spent helping me do mine. Please keep it up! ### Why all the talk about ggplot? -The word "ggplot" comes up a lot in discussions of plotting. Before I started using Python, I did most of my data analysis work in [R](https://cran.r-project.org/ "The Comprehensive R Archive Network"). I, with many Pythonistas, remain a big fan of Hadley Wickham's [ggplot2](http://ggplot2.org/ "ggplot2"), a "[grammar of graphics](https://www.amazon.com/Grammar-Graphics-Statistics-Computing/dp/0387245448 "The Grammar of Graphics (Statistics and Computing): Leland Wilkinson, D. Wills, D. Rope, A. Norton, R. Dubbs: 9780387245447: Amazon.com: Books")" implementation in R, for exploratory data analysis. +Before I started using Python, I did most of my data analysis work in [R](https://cran.r-project.org/ "The Comprehensive R Archive Network"). Like many Pythonistas, I remain a fan of Hadley Wickham's [ggplot2](https://ggplot2.tidyverse.org/ "ggplot2"), a "[grammar of graphics](https://www.amazon.com/Grammar-Graphics-Statistics-Computing/dp/0387245448 "The Grammar of Graphics")" implementation in R, for exploratory data analysis. -Like [scikit-learn](http://scikit-learn.org/ "scikit-learn: machine learning in Python") for machine learning in Python, ggplot2 provides a consistent API with sane defaults. The consistent interface makes it easier to iterate rapidly with low cognitive overhead. The sane defaults makes it easy to drop plots right into an email or presentation. +Like [scikit-learn](http://scikit-learn.org/ "scikit-learn: machine learning in Python") for machine learning in Python, ggplot2 has a consistent API and sane defaults. The consistent interface lets me iterate without stopping to think. The sane defaults make it easy to drop plots right into an email or presentation. -Particularly, ggplot2 allows the user to make basic plots (bar, histogram, line, scatter, density, violin) from data frames _with_ [faceting](http://ggplot2.tidyverse.org/reference/facet_grid.html) and [layering](https://rpubs.com/hadley/ggplot2-layers) by discrete values. +ggplot2 makes basic plots (bar, histogram, line, scatter, density, violin) from data frames _with_ [faceting](http://ggplot2.tidyverse.org/reference/facet_grid.html) and [layering](https://rpubs.com/hadley/ggplot2-layers) by discrete values. -An excellent introduction to the power of ggplot2 is in Hadley Wickham and Garrett Grolemund's book [R for Data Science](http://r4ds.had.co.nz/data-visualisation.html). +Hadley Wickham and Garrett Grolemund's [R for Data Science](http://r4ds.had.co.nz/data-visualisation.html) teaches ggplot2 well. ### Humble Rosetta Stone for Visualization in Exploratory Data Analysis -Below I have begun compiling a list of basic plots for exploratory data analysis. I have generated the plots with as many different libraries as time (and library) permits. +Below is a list of basic plots for exploratory data analysis, each made with as many libraries as time (and library) permit. -My hope is that this will (1) help you in your daily practice to work with what is available and (2) help inspire future development of Python plotting libraries. +I hope it helps you work with what exists today and inspires what gets built next. -Some rudimentary instructions on how you can contribute plots are [here](https://github.com/tdhopper/pythonplot.com#contributing). [General feedback or other plot suggestions](https://github.com/tdhopper/pythonplot.com/issues) are welcome. +[Contributing instructions](https://github.com/tdhopper/pythonplot.com#contributing) are on GitHub. [General feedback or other plot suggestions](https://github.com/tdhopper/pythonplot.com/issues) are welcome. #### Data -The datasets used below are included with ggplot2. One is the [Prices of 50,000 round cut diamonds](http://ggplot2.tidyverse.org/reference/diamonds.html) and the other is [Fuel economy data from 1999 and 2008 for 38 popular models of car](http://ggplot2.tidyverse.org/reference/mpg.html). +ggplot2 ships the datasets used below: the [Prices of 50,000 round cut diamonds](http://ggplot2.tidyverse.org/reference/diamonds.html) and [Fuel economy data from 1999 and 2008 for 38 popular models of car](http://ggplot2.tidyverse.org/reference/mpg.html). The time series example is a random walk I generate with a quick Python script. -Here's what a few rows of the datasets looks like: +A few rows of each: diff --git a/Makefile b/Makefile index 80afa27..71adbc1 100644 --- a/Makefile +++ b/Makefile @@ -1,40 +1,41 @@ GIT_COMMIT=$(shell git log -1 --pretty=format:"%h") OUTPUTDIR=web -S3_BUCKET=pythonplot.com - -all: render s3_upload +all: render clean: rm -f Examples.*.ipynb rm -f *.pyc rm -f .Rhistory + rm -f .setup_done -travis: render - -plotly_auth: - python ./.travis/authenticate_plotly.py - -test: - python -m pytest tests/ +.setup_done: + @echo "Setting up development environment..." + @echo "1. Installing Python dependencies with uv..." + @command -v uv >/dev/null 2>&1 || { echo "Installing uv..."; curl -LsSf https://astral.sh/uv/install.sh | sh; } + uv sync + @echo "2. Installing Chrome for Kaleido..." + @uv run kaleido_get_chrome || echo "Chrome may already be installed" + @echo "3. Installing R packages..." + @./setup_r.sh + @echo "✓ Setup complete!" + @touch .setup_done -qrender: - python render.py "Examples.ipynb" +setup: .setup_done -render: run_nb - python render.py "Examples.$(GIT_COMMIT).ipynb" +test: .setup_done + uv run pytest tests/ -s3_upload: - s3cmd sync $(OUTPUTDIR)/ s3://$(S3_BUCKET) --acl-public --delete-removed --guess-mime-type --no-mime-magic --no-preserve +qrender: .setup_done + uv run python render.py "Examples.ipynb" -run_nb: - jupyter nbconvert --to notebook --execute "Examples.ipynb" --output "Examples.$(GIT_COMMIT).ipynb" +render: .setup_done run_nb + uv run python render.py "Examples.$(GIT_COMMIT).ipynb" -dev_environment: - conda env update -q - source activate pythonplot && pip install -q -r requirements.txt +run_nb: .setup_done + uv run jupyter nbconvert --to notebook --execute "Examples.ipynb" --output "Examples.$(GIT_COMMIT).ipynb" -cloudfront_invalidate: - python .travis/invalidate_cloudfront.py +dev_environment: setup + @echo "Development environment ready!" -.PHONY: all qrender render s3_upload run_nb travis clean cloudfront_invalidate test dev_environment plotly_auth +.PHONY: all qrender render run_nb clean test dev_environment setup diff --git a/README.md b/README.md index 02be915..bd66fc2 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ By Tim Hopper: -[![Build Status](https://travis-ci.org/tdhopper/pythonplot.com.svg?branch=master)](https://travis-ci.org/tdhopper/pythonplot.com) +[![Build Status](https://github.com/tdhopper/pythonplot.com/actions/workflows/deploy.yml/badge.svg?branch=master)](https://github.com/tdhopper/pythonplot.com/actions/workflows/deploy.yml) ## An Introduction to Python Plotting for Exploratory Data Analysis @@ -49,7 +49,28 @@ Available at [pythonplot.com](http://pythonplot.com/) The site is generated from plots in the `Examples.ipynb` Python 3, Jupyter notebook. -You can create a [Conda](https://conda.io/docs/install/quick.html) dev environment to run the notebook with `make dev_environment`. +#### Development Setup + +**Requirements:** +- Python 3.11+ +- R 4.0+ (install via [Homebrew](https://brew.sh/) on macOS: `brew install r`, or from [CRAN](https://cran.r-project.org/)) +- [uv](https://github.com/astral-sh/uv) for Python package management + +**Quick Start:** +```bash +# Set up development environment (installs Python & R dependencies) +make dev_environment + +# Or manually: +# 1. Install uv +curl -LsSf https://astral.sh/uv/install.sh | sh + +# 2. Install Python dependencies +uv pip install -r requirements.txt + +# 3. Install R packages +./setup_r.sh +``` Each plot in the notebook is tagged with metadata using Jupyter cell tags. You can view the cell tags in your notebook with `View > Cell Toolbar > Tags`. @@ -76,8 +97,31 @@ If you are using Jupyter lab, these are available through the `Cell Tools` left The site layout current allows for ~46 characters in the code window. Please keep this in mind and wrap your code to avoid too much side scrolling. -At the moment, the code must return a png image into the output cell. To generate image files with `plotly`, using their server is currently the only way. Please see the [getting started page](https://plot.ly/python/getting-started/) for instructions on the `.credentials` file. +At the moment, the code must return a png image into the output cell. With modern versions of plotly (v5+), images are rendered locally using Kaleido - no authentication required. A Markdown comment can be added within triple quotes on the first line of the cell. (This currently doesn't work for `R` cells.) -You can render the images to `web/img/plots` by running `$ make`. Afterward, launch a local server (e.g. `python -m http.server`) from the `web` directory. \ No newline at end of file +#### Building the Site + +```bash +# Quick render (uses existing notebook without re-execution) +make qrender + +# Full build (executes notebook, then renders) +make render + +# Run tests +make test +``` + +After rendering, launch a local server from the `web` directory: +```bash +cd web && python -m http.server +``` + +#### CI/CD + +The project uses GitHub Actions for continuous integration and deployment: +- **Pushes to `master`**: Deployed to production on the Cloudflare Pages project `pythonplot` +- **Other branches**: Create preview deployments on Cloudflare Pages +- Requires secrets: `CLOUDFLARE_API_TOKEN` and `CLOUDFLARE_ACCOUNT_ID` \ No newline at end of file diff --git a/environment.yml b/environment.yml deleted file mode 100644 index 9e71f78..0000000 --- a/environment.yml +++ /dev/null @@ -1,11 +0,0 @@ -name: pythonplot -channels: - - r - - defaults - - plotly -dependencies: - - python>=3.6 - - pip - - mro-base - - r-ggplot2 - - plotly-orca diff --git a/netlify_deploy.sh b/netlify_deploy.sh deleted file mode 100755 index 4096afc..0000000 --- a/netlify_deploy.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -if [ $TRAVIS_BRANCH = "master" ]; then - netlify deploy \ - --auth $NETLIFY_AUTH_TOKEN \ - --site $NETLIFY_SITE_ID \ - --dir ./web \ - --prod \ - --message "Production deploy from Travis CI" -else - netlify deploy \ - --auth $NETLIFY_AUTH_TOKEN \ - --site $NETLIFY_SITE_ID \ - --dir ./web \ - --message "Preview deploy from Travis CI" -fi diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..e07bf92 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,61 @@ +[project] +name = "pythonplot" +version = "0.1.0" +description = "An Introduction to Python Plotting for Exploratory Data Analysis" +readme = "README.md" +requires-python = ">=3.11" + +dependencies = [ + # Core Jupyter/IPython + "ipython>=9.18.1", + "notebook>=7.6.5", + "jupyter>=1.0", + + # Data manipulation + "pandas>=3.1.1", + + # Plotting libraries + "matplotlib>=3.11.2", + "seaborn>=0.13", + "plotnine>=0.15.8", + "lets-plot>=4.11", + "plotly>=7.1.0", + "altair>=6.3.0", + "vl-convert-python>=1.0", # Required for Altair PNG export + "hvplot>=0.13.0", # Bokeh via the pandas .hvplot accessor + + # Image rendering + "kaleido>=1.5.1", # v1+ requires Chrome + "pillow>=13.0.0", + "selenium>=4.52.0", # Required for Bokeh PNG export + + # Statistical tools + "statsmodels>=0.15.0", + "scikit-misc>=0.5.3", + + # R integration + "rpy2>=3.6.9", + + # Template rendering + "jinja2>=3.1", + "jinja2-highlight>=0.6", + "markdown>=3.11.3", + + # Utilities + "psutil>=5.9", + "tzlocal>=5.4.5", + "simplegeneric>=0.8", + + # Cloud (if needed for deployment) + "boto3>=1.44.3", + + # Testing + "pytest>=9.2.0", +] + +[dependency-groups] +dev = [] + +[tool.pytest.ini_options] +testpaths = ["tests"] +python_files = ["test_*.py"] diff --git a/render.py b/render.py index 7d06749..c27c2c6 100644 --- a/render.py +++ b/render.py @@ -11,6 +11,8 @@ import subprocess import base64 import hashlib +import io +from PIL import Image logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') @@ -20,8 +22,11 @@ "pandas": "Pandas", "matplotlib": "Matplotlib", "seaborn": "Seaborn", + "seaborn-objects": "seaborn.objects", "plotnine": "plotnine", + "lets-plot": "lets-plot", "plotly": "plotly", + "hvplot": "hvPlot (Bokeh)", "altair": "Altair", "ggplot": "ggplot2 (R)", } @@ -29,6 +34,7 @@ names = { "bar-counts": "Bar Chart", "simple-histogram": "Histogram", + "box-plot": "Box Plot", "scatter-plot": "Scatter Plot", "timeseries": "Time Series", "scatter-plot-with-colors": "Scatter Plot with Faceted with Color", @@ -40,24 +46,36 @@ "stacked-bar-chart": "Stacked Bar Chart", "dodged-bar-chart": "Dodged Bar Chart", "stacked-kde": "Stacked KDE Plot", + "heatmap": "Heatmap", } with open("INTRO.md", "r") as f: intro = f.read() +WEBP_QUALITY = 85 + + def image_from_cell(cell): + """Save the cell's PNG output as WebP and return its path and pixel size. + + The filename stays the MD5 of the base64 PNG, so a plot that hasn't changed + keeps its URL. The pixel size lets the template declare width/height, which + reserves layout space for the lazily loaded images. + """ try: for c in cell['outputs']: if 'data' in c and 'image/png' in c['data']: base64_img = c['data']['image/png'].replace("\n", "").strip() filename = hashlib.md5() filename.update(base64_img.encode('ascii')) - web_path = "/img/plots/{}.png".format(filename.hexdigest()) + web_path = "/img/plots/{}.webp".format(filename.hexdigest()) full_path = "web" + web_path - with open(full_path, "wb") as fh: - fh.write(base64.b64decode(base64_img)) - return web_path + image = Image.open(io.BytesIO(base64.b64decode(base64_img))) + if image.mode not in ("RGB", "RGBA"): + image = image.convert("RGBA") + image.save(full_path, "WEBP", quality=WEBP_QUALITY, method=6) + return {"path": web_path, "width": image.width, "height": image.height} except KeyError as e: logging.error("Can't find image in cell: %s", cell['source']) raise e @@ -142,7 +160,9 @@ def extract_cells(path): "cell_num": cell_num, "package": packages.get(tags["package"], tags["package"]), "package-slug": tags['package'], - "image": image, + "image": image["path"], + "image-width": image["width"], + "image-height": image["height"], "content": source, "comment": md.convert(comment) or None, }) diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 44159dd..0000000 --- a/requirements.txt +++ /dev/null @@ -1,22 +0,0 @@ -selenium -pillow -ipython -pandas -jupyter -seaborn -plotnine -jinja2 -jinja2-highlight -markdown -scikit-misc -boto3 -pytest -chart-studio -statsmodels==0.10.0rc2 --pre -rpy2 -psutil -plotly -tzlocal -simplegeneric -altair -selenium diff --git a/runtime.txt b/runtime.txt deleted file mode 100644 index d70c8f8..0000000 --- a/runtime.txt +++ /dev/null @@ -1 +0,0 @@ -3.6 diff --git a/setup_r.sh b/setup_r.sh new file mode 100755 index 0000000..d2d0b1b --- /dev/null +++ b/setup_r.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +# Setup script for R and required R packages +# This script installs the R packages needed for pythonplot.com + +set -e # Exit on error + +echo "Installing R packages..." + +# Check if R is installed +if ! command -v R &> /dev/null; then + echo "Error: R is not installed." + echo "" + echo "Please install R first:" + echo " - macOS: brew install r" + echo " - Ubuntu/Debian: sudo apt-get install r-base r-base-dev" + echo " - Or download from: https://cran.r-project.org/" + exit 1 +fi + +echo "R version:" +R --version | head -n 1 + +# Install required R packages +R --quiet --no-save << 'EOF' +# Set CRAN mirror +options(repos = c(CRAN = "https://cran.rstudio.com/")) + +# List of required packages +packages <- c("ggplot2", "mgcv") + +# Install packages that aren't already installed +for (pkg in packages) { + if (!require(pkg, character.only = TRUE, quietly = TRUE)) { + cat(paste("Installing", pkg, "...\n")) + install.packages(pkg, quiet = FALSE) + } else { + cat(paste(pkg, "is already installed\n")) + } +} + +# Verify installations +cat("\nVerifying R package installations:\n") +for (pkg in packages) { + if (require(pkg, character.only = TRUE, quietly = TRUE)) { + cat(paste("✓", pkg, "version", as.character(packageVersion(pkg)), "\n")) + } else { + cat(paste("✗", pkg, "FAILED TO INSTALL\n")) + quit(status = 1) + } +} + +cat("\n✓ All R packages installed successfully!\n") +EOF + +echo "" +echo "R setup complete!" +echo "" +echo "Note: If you encounter issues with rpy2, make sure R_HOME is set correctly:" +echo " export R_HOME=\$(R RHOME)" diff --git a/templates/t_index.html b/templates/t_index.html index 106e589..0b65080 100644 --- a/templates/t_index.html +++ b/templates/t_index.html @@ -4,20 +4,28 @@ - + Python Plotting for Exploratory Analysis + + - - + + + + - + content="Side-by-side comparison of Python plotting libraries for exploratory data analysis: pandas, Matplotlib, Seaborn, plotnine, Lets-Plot, plotly, hvPlot, and Altair, with R's ggplot2 for reference." /> + + + + @@ -33,12 +41,8 @@ - - - + - - @@ -58,28 +62,27 @@ -
-
+

Python Plotting for Exploratory Data Analysis

@@ -101,7 +104,7 @@

Contents

-
+
{% for name, table in data.items() %}
{{ name }}
@@ -110,17 +113,17 @@
{{ name }}

-
+
{% for (name, slug), items in plots.items() %} + {% set outer_loop = loop %}
-

{{ name }}

-