{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pyhf\n", "import pandas\n", "import numpy as np\n", "import altair as alt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Visualization with Altair\n", "\n", "\n", "[Altair](https://altair-viz.github.io/) is a python API for generating [Vega](https://vega.github.io/) visuazliation specifications. We demonstracte how to use this to build an interactive chart of pyhf results." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Preparing the data\n", "\n", "Altair reads the data as a pandas dataframe, so we create one." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "model = pyhf.simplemodels.uncorrelated_background([7], [20], [5])\n", "data = [25] + model.config.auxdata" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "muscan = np.linspace(0, 5, 31)\n", "results = [\n", " pyhf.infer.hypotest(mu, data, model, return_expected_set=True) for mu in muscan\n", "]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | mu | \n", "obs | \n", "exp_0 | \n", "exp_1 | \n", "exp_2 | \n", "exp_3 | \n", "exp_4 | \n", "
---|---|---|---|---|---|---|---|
0 | \n", "0.000000 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "
1 | \n", "0.166667 | \n", "0.885208 | \n", "0.670809 | \n", "0.771258 | \n", "0.870322 | \n", "0.949235 | \n", "0.989385 | \n", "
2 | \n", "0.333333 | \n", "0.795986 | \n", "0.438838 | \n", "0.581516 | \n", "0.743696 | \n", "0.890881 | \n", "0.975022 | \n", "
3 | \n", "0.500000 | \n", "0.726450 | \n", "0.279981 | \n", "0.428500 | \n", "0.623443 | \n", "0.825621 | \n", "0.956105 | \n", "
4 | \n", "0.666667 | \n", "0.672216 | \n", "0.174235 | \n", "0.308524 | \n", "0.512383 | \n", "0.754629 | \n", "0.931866 | \n", "