|
16 | 16 | ] |
17 | 17 | }, |
18 | 18 | { |
19 | | - "cell_type": "markdown", |
20 | | - "id": "863c03b1-63e2-4d60-a3a4-4693afab4b5b", |
| 19 | + "cell_type": "raw", |
| 20 | + "id": "c72b5394-ff5f-42b1-b083-2e42b2ffdf0f", |
21 | 21 | "metadata": {}, |
22 | 22 | "source": [ |
23 | | - "Draw a single horizontal boxplot, assigning the data directly to the coordinate variable:" |
| 23 | + "The default violinplot represents a distribution two ways: a patch showing a symmetric kernel density estimate (KDE), and the quartiles / whiskers of a box plot:" |
24 | 24 | ] |
25 | 25 | }, |
26 | 26 | { |
|
35 | 35 | ] |
36 | 36 | }, |
37 | 37 | { |
38 | | - "cell_type": "markdown", |
39 | | - "id": "aeea380b-405e-4762-8ede-db57f5549ca5", |
| 38 | + "cell_type": "raw", |
| 39 | + "id": "e7d25589-0dc9-48ce-92f9-ab61ffbf964a", |
40 | 40 | "metadata": {}, |
41 | 41 | "source": [ |
42 | | - "Group by a categorical variable, referencing columns in a dataframe:" |
| 42 | + "In a bivariate plot, one of the variables will \"group\" so that multiple violins are drawn:" |
43 | 43 | ] |
44 | 44 | }, |
45 | 45 | { |
|
53 | 53 | ] |
54 | 54 | }, |
55 | 55 | { |
56 | | - "cell_type": "markdown", |
57 | | - "id": "c9a99aa4-2da0-42fa-879a-0c3b264803f4", |
| 56 | + "cell_type": "raw", |
| 57 | + "id": "6d588b32-b14b-4b33-bbd9-69b17f8212a6", |
58 | 58 | "metadata": {}, |
59 | 59 | "source": [ |
60 | | - "Draw vertical violins, grouped by two variables:" |
| 60 | + "By default, the orientation of the plot is determined by the variable types, preferring to group by a categorical variable:" |
61 | 61 | ] |
62 | 62 | }, |
63 | 63 | { |
|
71 | 71 | ] |
72 | 72 | }, |
73 | 73 | { |
74 | | - "cell_type": "markdown", |
75 | | - "id": "973e6617-5720-428d-a0ac-447e76aa9fde", |
| 74 | + "cell_type": "raw", |
| 75 | + "id": "402812f2-c024-4179-9fee-fed92f03deb2", |
76 | 76 | "metadata": {}, |
77 | 77 | "source": [ |
78 | | - "Draw split violins to take up less space:" |
| 78 | + "Pass `fill=False to draw line-art violins:" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "execution_count": null, |
| 84 | + "id": "8e00ce8b-5871-486b-8c55-a4f2e764aa86", |
| 85 | + "metadata": {}, |
| 86 | + "outputs": [], |
| 87 | + "source": [ |
| 88 | + "sns.violinplot(data=df, x=\"class\", y=\"age\", hue=\"alive\", fill=False)" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "raw", |
| 93 | + "id": "8350abce-6a40-4e18-9501-7d358192471b", |
| 94 | + "metadata": {}, |
| 95 | + "source": [ |
| 96 | + "Draw \"split\" violins to take up less space, and only show the data quarties:" |
79 | 97 | ] |
80 | 98 | }, |
81 | 99 | { |
|
85 | 103 | "metadata": {}, |
86 | 104 | "outputs": [], |
87 | 105 | "source": [ |
88 | | - "sns.violinplot(data=df, x=\"deck\", y=\"age\", hue=\"alive\", split=True)" |
| 106 | + "sns.violinplot(data=df, x=\"class\", y=\"age\", hue=\"alive\", split=True, inner=\"quart\")" |
89 | 107 | ] |
90 | 108 | }, |
91 | 109 | { |
92 | | - "cell_type": "markdown", |
93 | | - "id": "f291d4a2-41bc-4eb0-813d-7a1ceacc0cb0", |
| 110 | + "cell_type": "raw", |
| 111 | + "id": "90f4263f-7294-4ad5-bff4-25d7d796cb45", |
94 | 112 | "metadata": {}, |
95 | 113 | "source": [ |
96 | | - "Prevent the density from smoothing beyond the limits of the data:" |
| 114 | + "Add a small gap between the dodged violins:" |
97 | 115 | ] |
98 | 116 | }, |
99 | 117 | { |
100 | 118 | "cell_type": "code", |
101 | 119 | "execution_count": null, |
102 | | - "id": "82556de0-3756-426c-a591-9af6ed6c45d4", |
| 120 | + "id": "26cb5b89-496d-4893-8914-ca8b6fbf97b7", |
103 | 121 | "metadata": {}, |
104 | 122 | "outputs": [], |
105 | 123 | "source": [ |
106 | | - "sns.violinplot(data=df, x=\"age\", y=\"alive\", cut=0)" |
| 124 | + "sns.violinplot(data=df, x=\"class\", y=\"age\", hue=\"alive\", split=True, gap=.1, inner=\"quart\")" |
107 | 125 | ] |
108 | 126 | }, |
109 | 127 | { |
110 | | - "cell_type": "markdown", |
111 | | - "id": "6f351f71-1db3-4c5a-948c-9e1dbc550234", |
| 128 | + "cell_type": "raw", |
| 129 | + "id": "bbea49e0-7b08-4b25-8686-1d5404b71601", |
112 | 130 | "metadata": {}, |
113 | 131 | "source": [ |
114 | | - "Use a narrower bandwidth to reduce the amount of smoothing:" |
| 132 | + "Starting in version 0.13.0, it is possilbe to \"split\" single violins:" |
115 | 133 | ] |
116 | 134 | }, |
117 | 135 | { |
118 | 136 | "cell_type": "code", |
119 | 137 | "execution_count": null, |
120 | | - "id": "8d17e1e3-e0f4-4d2c-ac6e-aec42ed75390", |
| 138 | + "id": "ba261531-a280-44e5-b8c0-bcc5a53f60bf", |
121 | 139 | "metadata": {}, |
122 | 140 | "outputs": [], |
123 | 141 | "source": [ |
124 | | - "sns.violinplot(data=df, x=\"age\", y=\"alive\", bw=.15)" |
| 142 | + "sns.violinplot(data=df, x=\"class\", y=\"age\", split=True, inner=\"quart\")" |
125 | 143 | ] |
126 | 144 | }, |
127 | 145 | { |
128 | | - "cell_type": "markdown", |
129 | | - "id": "c4aaeb60-6c1b-4337-91ce-d6b744a3dd90", |
| 146 | + "cell_type": "raw", |
| 147 | + "id": "7c4dafa1-2747-4b43-ba4a-4c9b32778086", |
130 | 148 | "metadata": {}, |
131 | 149 | "source": [ |
132 | | - "Represent every observation inside the distribution" |
| 150 | + "Represent every observation inside the distribution by setting `inner=\"stick\"` or `inner=\"point\"`:" |
133 | 151 | ] |
134 | 152 | }, |
135 | 153 | { |
|
139 | 157 | "metadata": {}, |
140 | 158 | "outputs": [], |
141 | 159 | "source": [ |
142 | | - "sns.violinplot(data=df, x=\"age\", y=\"embark_town\", inner=\"stick\")" |
| 160 | + "sns.violinplot(data=df, x=\"age\", y=\"deck\", inner=\"point\")" |
143 | 161 | ] |
144 | 162 | }, |
145 | 163 | { |
146 | | - "cell_type": "markdown", |
147 | | - "id": "01622556-9df8-4af1-b36c-9bc5f6b6099e", |
| 164 | + "cell_type": "raw", |
| 165 | + "id": "23c13695-cd01-4da8-bc89-2519ae445f9f", |
148 | 166 | "metadata": {}, |
149 | 167 | "source": [ |
150 | | - "Use a different scaling rule for normalizing the density:" |
| 168 | + "Normalize the width of each violin to represent the number of observations:" |
151 | 169 | ] |
152 | 170 | }, |
153 | 171 | { |
|
157 | 175 | "metadata": {}, |
158 | 176 | "outputs": [], |
159 | 177 | "source": [ |
160 | | - "sns.violinplot(data=df, x=\"age\", y=\"embark_town\", scale=\"count\")" |
| 178 | + "sns.violinplot(data=df, x=\"age\", y=\"deck\", inner=\"point\", density_norm=\"count\")" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "cell_type": "raw", |
| 183 | + "id": "abe650fb-4d26-4bac-97f3-f451a3872cf5", |
| 184 | + "metadata": {}, |
| 185 | + "source": [ |
| 186 | + "By default, the KDE will smooth past the extremes of the observed data; set `cut=0` to prevent this:" |
| 187 | + ] |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "code", |
| 191 | + "execution_count": null, |
| 192 | + "id": "82556de0-3756-426c-a591-9af6ed6c45d4", |
| 193 | + "metadata": {}, |
| 194 | + "outputs": [], |
| 195 | + "source": [ |
| 196 | + "sns.violinplot(data=df, x=\"age\", y=\"alive\", cut=0, inner=\"stick\")" |
| 197 | + ] |
| 198 | + }, |
| 199 | + { |
| 200 | + "cell_type": "raw", |
| 201 | + "id": "abfb9e78-d524-4536-90ef-c71834b055f9", |
| 202 | + "metadata": {}, |
| 203 | + "source": [ |
| 204 | + "The `bw_adjust` parameter controls the amount of smoothing:" |
| 205 | + ] |
| 206 | + }, |
| 207 | + { |
| 208 | + "cell_type": "code", |
| 209 | + "execution_count": null, |
| 210 | + "id": "8d17e1e3-e0f4-4d2c-ac6e-aec42ed75390", |
| 211 | + "metadata": {}, |
| 212 | + "outputs": [], |
| 213 | + "source": [ |
| 214 | + "sns.violinplot(data=df, x=\"age\", y=\"alive\", bw_adjust=.5, inner=\"stick\")" |
| 215 | + ] |
| 216 | + }, |
| 217 | + { |
| 218 | + "cell_type": "raw", |
| 219 | + "id": "407bc513-5b7f-418c-8ffe-ec488836586d", |
| 220 | + "metadata": {}, |
| 221 | + "source": [ |
| 222 | + "By default, the violins are drawn at fixed positions on a categorical scale, even if the grouping variable is numeric. Starting in version 0.13.0, pass the `native_scale=True` parameter to preserve the original scale on both axes:" |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "code", |
| 227 | + "execution_count": null, |
| 228 | + "id": "e7b6d901-9a97-4716-8d24-1b30145e9c57", |
| 229 | + "metadata": {}, |
| 230 | + "outputs": [], |
| 231 | + "source": [ |
| 232 | + "sns.violinplot(x=df[\"age\"].round(-1) + 5, y=df[\"fare\"], native_scale=True)" |
| 233 | + ] |
| 234 | + }, |
| 235 | + { |
| 236 | + "cell_type": "raw", |
| 237 | + "id": "790e3989-0b47-4e77-9bdb-dc757d1e938c", |
| 238 | + "metadata": {}, |
| 239 | + "source": [ |
| 240 | + "When using a categorical scale, the `formatter` parameter accepts a function that defines categories:" |
| 241 | + ] |
| 242 | + }, |
| 243 | + { |
| 244 | + "cell_type": "code", |
| 245 | + "execution_count": null, |
| 246 | + "id": "28a769d4-3e23-4b53-a9ef-391d5fc24201", |
| 247 | + "metadata": {}, |
| 248 | + "outputs": [], |
| 249 | + "source": [ |
| 250 | + "decades = lambda x: f\"{int(x)}–{int(x + 10)}\"\n", |
| 251 | + "sns.violinplot(x=df[\"age\"].round(-1), y=df[\"fare\"], formatter=decades)" |
| 252 | + ] |
| 253 | + }, |
| 254 | + { |
| 255 | + "cell_type": "raw", |
| 256 | + "id": "6f914d73-7a0c-4fbc-8432-40c4f0577857", |
| 257 | + "metadata": {}, |
| 258 | + "source": [ |
| 259 | + "By default, the \"inner\" representation scales with the `linewidth` and `linecolor` parameters:" |
| 260 | + ] |
| 261 | + }, |
| 262 | + { |
| 263 | + "cell_type": "code", |
| 264 | + "execution_count": null, |
| 265 | + "id": "18cb2afd-8487-40bd-b3f2-1f83243ffa3c", |
| 266 | + "metadata": {}, |
| 267 | + "outputs": [], |
| 268 | + "source": [ |
| 269 | + "sns.violinplot(data=df, x=\"age\", linewidth=1, linecolor=\"k\")" |
| 270 | + ] |
| 271 | + }, |
| 272 | + { |
| 273 | + "cell_type": "raw", |
| 274 | + "id": "ca2ef541-c07f-4853-ba98-ce75855ba262", |
| 275 | + "metadata": {}, |
| 276 | + "source": [ |
| 277 | + "Use `inner_kws` to pass parameters directly to the inner plotting functions:" |
| 278 | + ] |
| 279 | + }, |
| 280 | + { |
| 281 | + "cell_type": "code", |
| 282 | + "execution_count": null, |
| 283 | + "id": "934f91bc-2698-4c07-92cf-4e6039c801b2", |
| 284 | + "metadata": {}, |
| 285 | + "outputs": [], |
| 286 | + "source": [ |
| 287 | + "sns.violinplot(data=df, x=\"age\", inner_kws=dict(box_width=15, whis_width=2, color=\".8\"))" |
161 | 288 | ] |
162 | 289 | }, |
163 | 290 | { |
164 | 291 | "cell_type": "code", |
165 | 292 | "execution_count": null, |
166 | | - "id": "fdda9a33-37f3-43fd-b02d-1ff414657a37", |
| 293 | + "id": "4aa00d3c-f016-4db8-b6b0-da4e6a327831", |
167 | 294 | "metadata": {}, |
168 | 295 | "outputs": [], |
169 | 296 | "source": [] |
|
0 commit comments