|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "dcc1ae12-bba4-4de9-af8d-543b3d65b42b", |
| 7 | + "metadata": { |
| 8 | + "tags": [ |
| 9 | + "hide" |
| 10 | + ] |
| 11 | + }, |
| 12 | + "outputs": [], |
| 13 | + "source": [ |
| 14 | + "import seaborn.objects as so\n", |
| 15 | + "from seaborn import load_dataset\n", |
| 16 | + "penguins = load_dataset(\"penguins\")" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "raw", |
| 21 | + "id": "1042b991-1471-43bd-934c-43caae3cb2fa", |
| 22 | + "metadata": {}, |
| 23 | + "source": [ |
| 24 | + "This stat estimates transforms observations into a smooth function representing the estimated density:" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": null, |
| 30 | + "id": "2406e2aa-7f0f-4a51-af59-4cef827d28d8", |
| 31 | + "metadata": {}, |
| 32 | + "outputs": [], |
| 33 | + "source": [ |
| 34 | + "p = so.Plot(penguins, x=\"flipper_length_mm\")\n", |
| 35 | + "p.add(so.Area(), so.KDE())" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "raw", |
| 40 | + "id": "44515f21-683b-420f-967b-4c7568c907d7", |
| 41 | + "metadata": {}, |
| 42 | + "source": [ |
| 43 | + "Adjust the smoothing bandwidth to see more or fewer details:" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": null, |
| 49 | + "id": "d4e6ba5b-4dd2-4210-8cf0-de057dc71e2a", |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [], |
| 52 | + "source": [ |
| 53 | + "p.add(so.Area(), so.KDE(bw_adjust=0.25))" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "raw", |
| 58 | + "id": "fd665fe1-a5e4-4742-adc9-e40615d57d08", |
| 59 | + "metadata": {}, |
| 60 | + "source": [ |
| 61 | + "The curve will extend beyond observed values in the dataset:" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": null, |
| 67 | + "id": "4cda1cb8-f663-4f94-aa24-6f1727a41031", |
| 68 | + "metadata": {}, |
| 69 | + "outputs": [], |
| 70 | + "source": [ |
| 71 | + "p2 = p.add(so.Bars(alpha=.3), so.Hist(\"density\"))\n", |
| 72 | + "p2.add(so.Line(), so.KDE())" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "raw", |
| 77 | + "id": "75235825-d522-4562-aacc-9b7413eabf5d", |
| 78 | + "metadata": {}, |
| 79 | + "source": [ |
| 80 | + "Control the range of the density curve relative to the observations using `cut`:" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": null, |
| 86 | + "id": "a7a9275e-9889-437d-bdc5-18653d2c92ef", |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [], |
| 89 | + "source": [ |
| 90 | + "p2.add(so.Line(), so.KDE(cut=0))" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "raw", |
| 95 | + "id": "6a885eeb-81ba-47c6-8402-1bef40544fd1", |
| 96 | + "metadata": {}, |
| 97 | + "source": [ |
| 98 | + "When observations are assigned to the `y` variable, the density will be shown for `x`:" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": null, |
| 104 | + "id": "38b3a0fb-54ff-493a-bd64-f83a12365723", |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [], |
| 107 | + "source": [ |
| 108 | + "so.Plot(penguins, y=\"flipper_length_mm\").add(so.Area(), so.KDE())" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "raw", |
| 113 | + "id": "59996340-168e-479f-a0c6-c7e1fcab0fb0", |
| 114 | + "metadata": {}, |
| 115 | + "source": [ |
| 116 | + "Use `gridsize` to increase or decrease the resolution of the grid where the density is evaluated:" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "code", |
| 121 | + "execution_count": null, |
| 122 | + "id": "23715820-7df9-40ba-9e74-f11564704dd0", |
| 123 | + "metadata": {}, |
| 124 | + "outputs": [], |
| 125 | + "source": [ |
| 126 | + "p.add(so.Dots(), so.KDE(gridsize=100))" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "raw", |
| 131 | + "id": "4c9b6492-98c8-45ab-9f53-681cde2f767a", |
| 132 | + "metadata": {}, |
| 133 | + "source": [ |
| 134 | + "Or pass `None` to evaluate the density at the original datapoints:" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": null, |
| 140 | + "id": "4e1b6810-5c28-43aa-aa61-652521299b51", |
| 141 | + "metadata": {}, |
| 142 | + "outputs": [], |
| 143 | + "source": [ |
| 144 | + "p.add(so.Dots(), so.KDE(gridsize=None))" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "raw", |
| 149 | + "id": "0970a56b-0cba-4c40-bb1b-b8e71739df5c", |
| 150 | + "metadata": {}, |
| 151 | + "source": [ |
| 152 | + "Other variables will define groups for the estimation:" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "execution_count": null, |
| 158 | + "id": "5f0ce0b6-5742-4bc0-9ac3-abedde923684", |
| 159 | + "metadata": {}, |
| 160 | + "outputs": [], |
| 161 | + "source": [ |
| 162 | + "p.add(so.Area(), so.KDE(), color=\"species\")" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "raw", |
| 167 | + "id": "22204fcd-4b25-46e5-a170-02b1419c23d5", |
| 168 | + "metadata": {}, |
| 169 | + "source": [ |
| 170 | + "By default, the density is normalized across all groups (i.e., the joint density is shown); pass `common_norm=False` to show conditional densities:" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "code", |
| 175 | + "execution_count": null, |
| 176 | + "id": "6ad56958-dc45-4632-94d1-23039ad3ec58", |
| 177 | + "metadata": {}, |
| 178 | + "outputs": [], |
| 179 | + "source": [ |
| 180 | + "p.add(so.Area(), so.KDE(common_norm=False), color=\"species\")" |
| 181 | + ] |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "raw", |
| 185 | + "id": "b1627197-85d1-4476-b4ae-3e93044ee988", |
| 186 | + "metadata": {}, |
| 187 | + "source": [ |
| 188 | + "Or pass a list of variables to condition on:" |
| 189 | + ] |
| 190 | + }, |
| 191 | + { |
| 192 | + "cell_type": "code", |
| 193 | + "execution_count": null, |
| 194 | + "id": "58f63734-5afd-4d90-bbfb-fc39c8d1981f", |
| 195 | + "metadata": {}, |
| 196 | + "outputs": [], |
| 197 | + "source": [ |
| 198 | + "(\n", |
| 199 | + " p.facet(\"sex\")\n", |
| 200 | + " .add(so.Area(), so.KDE(common_norm=[\"col\"]), color=\"species\")\n", |
| 201 | + ")" |
| 202 | + ] |
| 203 | + }, |
| 204 | + { |
| 205 | + "cell_type": "raw", |
| 206 | + "id": "2b7e018e-1374-4939-909c-e95f5ffd086e", |
| 207 | + "metadata": {}, |
| 208 | + "source": [ |
| 209 | + "This stat can be combined with other transforms, such as :class:`Stack` (when `common_grid=True`):" |
| 210 | + ] |
| 211 | + }, |
| 212 | + { |
| 213 | + "cell_type": "code", |
| 214 | + "execution_count": null, |
| 215 | + "id": "96e5b2d0-c7e2-47df-91f1-7f9ec0bb08a9", |
| 216 | + "metadata": {}, |
| 217 | + "outputs": [], |
| 218 | + "source": [ |
| 219 | + "p.add(so.Area(), so.KDE(), so.Stack(), color=\"sex\")" |
| 220 | + ] |
| 221 | + }, |
| 222 | + { |
| 223 | + "cell_type": "raw", |
| 224 | + "id": "8500ff86-0b1f-4831-954b-08b6df690387", |
| 225 | + "metadata": {}, |
| 226 | + "source": [ |
| 227 | + "Set `cumulative=True` to integrate the density:" |
| 228 | + ] |
| 229 | + }, |
| 230 | + { |
| 231 | + "cell_type": "code", |
| 232 | + "execution_count": null, |
| 233 | + "id": "26bb736e-7cfd-421e-b80d-42fa450e88c0", |
| 234 | + "metadata": {}, |
| 235 | + "outputs": [], |
| 236 | + "source": [ |
| 237 | + "p.add(so.Line(), so.KDE(cumulative=True))" |
| 238 | + ] |
| 239 | + }, |
| 240 | + { |
| 241 | + "cell_type": "code", |
| 242 | + "execution_count": null, |
| 243 | + "id": "e8bfd9d2-ad60-4971-aa7f-71a285f44a20", |
| 244 | + "metadata": {}, |
| 245 | + "outputs": [], |
| 246 | + "source": [] |
| 247 | + } |
| 248 | + ], |
| 249 | + "metadata": { |
| 250 | + "kernelspec": { |
| 251 | + "display_name": "py310", |
| 252 | + "language": "python", |
| 253 | + "name": "py310" |
| 254 | + }, |
| 255 | + "language_info": { |
| 256 | + "codemirror_mode": { |
| 257 | + "name": "ipython", |
| 258 | + "version": 3 |
| 259 | + }, |
| 260 | + "file_extension": ".py", |
| 261 | + "mimetype": "text/x-python", |
| 262 | + "name": "python", |
| 263 | + "nbconvert_exporter": "python", |
| 264 | + "pygments_lexer": "ipython3", |
| 265 | + "version": "3.10.0" |
| 266 | + } |
| 267 | + }, |
| 268 | + "nbformat": 4, |
| 269 | + "nbformat_minor": 5 |
| 270 | +} |
0 commit comments