{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Custom plotting\n", "\n", "It can sometimes be useful to add custom annotations or plot elements to the output of `corner.corner`.\n", "This tutorial shows how you can do this.\n", "\n", "For example, let's say that we want to overplot a few different values for comparison.\n", "The `truths` interface isn't sufficient for supporting this use case, so here's how you can do it.\n", "\n", "First, let's generate some fake data with a mode at the origin and another randomly sampled mode:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import corner\n", "import numpy as np\n", "\n", "ndim, nsamples = 4, 50000\n", "np.random.seed(1234)\n", "data1 = np.random.randn(ndim * 4 * nsamples // 5).reshape(\n", " [4 * nsamples // 5, ndim]\n", ")\n", "mean = 4 * np.random.rand(ndim)\n", "data2 = mean[None, :] + np.random.randn(ndim * nsamples // 5).reshape(\n", " [nsamples // 5, ndim]\n", ")\n", "samples = np.vstack([data1, data2])\n", "\n", "figure = corner.corner(samples)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's overplot the empirical mean of the samples and the true mean of the second mode." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This is the true mean of the second mode that we used above:\n", "value1 = mean\n", "\n", "# This is the empirical mean of the sample:\n", "value2 = np.mean(samples, axis=0)\n", "\n", "# Make the base corner plot\n", "figure = corner.corner(samples)\n", "\n", "# Extract the axes\n", "axes = np.array(figure.axes).reshape((ndim, ndim))\n", "\n", "# Loop over the diagonal\n", "for i in range(ndim):\n", " ax = axes[i, i]\n", " ax.axvline(value1[i], color=\"g\")\n", " ax.axvline(value2[i], color=\"r\")\n", "\n", "# Loop over the histograms\n", "for yi in range(ndim):\n", " for xi in range(yi):\n", " ax = axes[yi, xi]\n", " ax.axvline(value1[xi], color=\"g\")\n", " ax.axvline(value2[xi], color=\"r\")\n", " ax.axhline(value1[yi], color=\"g\")\n", " ax.axhline(value2[yi], color=\"r\")\n", " ax.plot(value1[xi], value1[yi], \"sg\")\n", " ax.plot(value2[xi], value2[yi], \"sr\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A similar procedure could be used to add anything to the axes that you can normally do with matplotlib.\n", "\n", "This being said, there is actually an even easier way to do this using the `overplot_lines` and `overplot_points` functions:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This is the true mean of the second mode that we used above:\n", "value1 = mean\n", "\n", "# This is the empirical mean of the sample:\n", "value2 = np.mean(samples, axis=0)\n", "\n", "# Make the base corner plot\n", "figure = corner.corner(samples)\n", "\n", "corner.overplot_lines(figure, value1, color=\"C1\")\n", "corner.overplot_points(figure, value1[None], marker=\"s\", color=\"C1\")\n", "corner.overplot_lines(figure, value2, color=\"C2\")\n", "corner.overplot_points(figure, value2[None], marker=\"s\", color=\"C2\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "python", "version": "3.9.1" } }, "nbformat": 4, "nbformat_minor": 4 }