| 1 |
%% tikzviolinplots.sty |
| 2 |
%% Copyright 2026 Pedro Callil-Soares |
| 3 |
% |
| 4 |
% This work may be distributed and/or modified under the |
| 5 |
% conditions of the LaTeX Project Public License, either version 1.3 |
| 6 |
% of this license or (at your option) any later version. |
| 7 |
% The latest version of this license is in |
| 8 |
% http://www.latex-project.org/lppl.txt |
| 9 |
% and version 1.3 or later is part of all distributions of LaTeX |
| 10 |
% version 2005/12/01 or later. |
| 11 |
% |
| 12 |
% This work has the LPPL maintenance status `maintained'. |
| 13 |
% |
| 14 |
% The Current Maintainer of this work is Pedro Callil-Soares. |
| 15 |
% |
| 16 |
% This work consists of the files tikzviolinplots.sty and |
| 17 |
% tikzviolinplots.tex. |
| 18 |
|
| 19 |
\NeedsTeXFormat{LaTeX2e}[1994/06/01] |
| 20 |
\ProvidesPackage{tikzviolinplots}[2026/03/31 v0.11.1 Violin plot creation in pgfplots] |
| 21 |
|
| 22 |
\RequirePackage{pgfplots} |
| 23 |
\RequirePackage{pgfplotstable} |
| 24 |
\RequirePackage{pgfkeys} |
| 25 |
|
| 26 |
|
| 27 |
\DeclareOption*{\PackageWarning{tikzviolinplots}{Unknown option: ‘\CurrentOption’}} |
| 28 |
\ProcessOptions\relax |
| 29 |
|
| 30 |
\newif\ifviolin@scaled |
| 31 |
\newif\ifviolin@datapoints |
| 32 |
\newif\ifviolin@averages |
| 33 |
\newif\ifviolin@nomirror |
| 34 |
\newif\ifviolin@reverseaxis |
| 35 |
\newif\ifviolin@invert |
| 36 |
|
| 37 |
\pgfkeys{ |
| 38 |
/violinplot/.is family, |
| 39 |
/violinplot/.search also=/pgfplots, |
| 40 |
/violinplot, |
| 41 |
% |
| 42 |
scaled/.is if=violin@scaled, |
| 43 |
data points/.is if=violin@datapoints, |
| 44 |
averages/.is if=violin@averages, |
| 45 |
no mirror/.is if=violin@nomirror, |
| 46 |
reverse axis/.is if=violin@reverseaxis, |
| 47 |
invert/.is if=violin@invert, |
| 48 |
% |
| 49 |
xmin/.value required, |
| 50 |
xmax/.value required, |
| 51 |
ymin/.value required, |
| 52 |
ymax/.value required, |
| 53 |
col sep/.value required, |
| 54 |
kernel/.value required, |
| 55 |
bandwidth/.value required, |
| 56 |
index/.value required, |
| 57 |
indexes/.value required, |
| 58 |
frequencies/.value required, |
| 59 |
samples/.value required, |
| 60 |
relative position/.value required, |
| 61 |
spacing/.value required, |
| 62 |
color/.value required, |
| 63 |
primary color/.value required, |
| 64 |
secondary color/.value required, |
| 65 |
fill opacity/.value required, |
| 66 |
label/.value required, |
| 67 |
labels/.value required, |
| 68 |
data label style/.value required, |
| 69 |
average mark/.value required, |
| 70 |
average size/.value required, |
| 71 |
average color/.value required, |
| 72 |
average opacity/.value required, |
| 73 |
average fill/.value required, |
| 74 |
average fill opacity/.value required, |
| 75 |
dataset mark/.value required, |
| 76 |
dataset size/.value required, |
| 77 |
dataset color/.value required, |
| 78 |
dataset opacity/.value required, |
| 79 |
dataset fill/.value required, |
| 80 |
dataset fill opacity/.value required, |
| 81 |
dataset jitter/.value required, |
| 82 |
% |
| 83 |
col sep/.is choice, |
| 84 |
col sep/space/.code=\def\violin@colsep{space}, |
| 85 |
col sep/tab/.code=\def\violin@colsep{tab}, |
| 86 |
col sep/comma/.code=\def\violin@colsep{comma}, |
| 87 |
col sep/colon/.code=\def\violin@colsep{colon}, |
| 88 |
col sep/semicolon/.code=\def\violin@colsep{semicolon}, |
| 89 |
col sep/braces/.code=\def\violin@colsep{braces}, |
| 90 |
col sep/ampersand/.code=\def\violin@colsep{ampersand}, |
| 91 |
% |
| 92 |
kernel/.is choice, |
| 93 |
kernel/gaussian/.code=\def\violin@kernel{gaussian}, |
| 94 |
kernel/logistic/.code=\def\violin@kernel{logistic}, |
| 95 |
kernel/parabolic/.code=\def\violin@kernel{parabolic}, |
| 96 |
kernel/uniform/.code=\def\violin@kernel{uniform}, |
| 97 |
kernel/triangular/.code=\def\violin@kernel{triangular}, |
| 98 |
% |
| 99 |
xmin/.store in = \violin@xmin, |
| 100 |
xmax/.store in = \violin@xmax, |
| 101 |
ymin/.store in = \violin@ymin, |
| 102 |
ymax/.store in = \violin@ymax, |
| 103 |
bandwidth/.store in = \violin@bandwidth, |
| 104 |
index/.store in = \violin@index, |
| 105 |
indexes/.store in = \violin@indexes, |
| 106 |
frequencies/.store in = \violin@frequencies, |
| 107 |
samples/.store in = \violin@samples, |
| 108 |
relative position/.store in = \violin@delta, |
| 109 |
spacing/.store in = \violin@spacing, |
| 110 |
color/.store in = \violin@color, |
| 111 |
primary color/.store in = \violin@color@primary, |
| 112 |
secondary color/.store in = \violin@color@secondary, |
| 113 |
fill opacity/.store in = \violin@fillopacity, |
| 114 |
label/.store in = \violin@label, |
| 115 |
labels/.store in = \violin@labels, |
| 116 |
data label style/.store in = \violin@datalabelstyle, |
| 117 |
average mark/.store in = \violin@avg@mark, |
| 118 |
average size/.store in = \violin@avg@size, |
| 119 |
average color/.store in = \violin@avg@color, |
| 120 |
average opacity/.store in = \violin@avg@opacity, |
| 121 |
average fill/.store in = \violin@avg@fillcolor, |
| 122 |
average fill opacity/.store in = \violin@avg@fillopacity, |
| 123 |
dataset mark/.store in = \violin@pts@mark, |
| 124 |
dataset size/.store in = \violin@pts@size, |
| 125 |
dataset color/.store in = \violin@pts@color, |
| 126 |
dataset opacity/.store in = \violin@pts@opacity, |
| 127 |
dataset fill/.store in = \violin@pts@fillcolor, |
| 128 |
dataset fill opacity/.store in = \violin@pts@fillopacity, |
| 129 |
dataset jitter/.store in = \violin@pts@jitter, |
| 130 |
% |
| 131 |
xmin=-10, |
| 132 |
xmax=10, |
| 133 |
ymin=-10, |
| 134 |
ymax=10, |
| 135 |
% |
| 136 |
xmin/.forward to = {/pgfplots/xmin}, |
| 137 |
xmax/.forward to = {/pgfplots/xmax}, |
| 138 |
ymin/.forward to = {/pgfplots/ymin}, |
| 139 |
ymax/.forward to = {/pgfplots/ymax}, |
| 140 |
% |
| 141 |
default/.style = { |
| 142 |
col sep=comma, |
| 143 |
kernel=gaussian, |
| 144 |
bandwidth=NONE, |
| 145 |
index=DATA, |
| 146 |
indexes={A,B,C}, |
| 147 |
frequencies=FREQINTERNALVIOLIN, |
| 148 |
samples=50, |
| 149 |
relative position=0, |
| 150 |
spacing=1.0, |
| 151 |
color=black, |
| 152 |
primary color=blue, |
| 153 |
secondary color=black, |
| 154 |
fill opacity=0.5, |
| 155 |
label={LABEL}, |
| 156 |
labels={A,B,C}, |
| 157 |
data label style={}, |
| 158 |
invert=false, |
| 159 |
average mark=x, |
| 160 |
average size=3pt, |
| 161 |
average color=black, |
| 162 |
average opacity=1.0, |
| 163 |
average fill=white, |
| 164 |
average fill opacity=0.5, |
| 165 |
dataset mark=*, |
| 166 |
dataset size=1pt, |
| 167 |
dataset color=black, |
| 168 |
dataset opacity=1.0, |
| 169 |
dataset fill=black, |
| 170 |
dataset fill opacity=0.2, |
| 171 |
dataset jitter=0.0, |
| 172 |
}, |
| 173 |
default, |
| 174 |
} |
| 175 |
|
| 176 |
\newcommand{\violinsetoptions}[2][]{% |
| 177 |
\pgfkeys@spdef\violin@violinargs{#1} |
| 178 |
\pgfkeys@spdef\violin@pgfplotsargs{#2} |
| 179 |
\edef\violin@args{ |
| 180 |
/handler config=only existing, |
| 181 |
/violinplot, |
| 182 |
\expandonce{\violin@violinargs}, |
| 183 |
\expandonce{\violin@pgfplotsargs}, |
| 184 |
/handler config=all, |
| 185 |
} |
| 186 |
\pgfkeysalsofrom{\violin@args} |
| 187 |
|
| 188 |
\pgfmathqparse{0} |
| 189 |
\let\violin@numofplots\pgfmathresult % Needed to distinguish first plot from others in \violinplot |
| 190 |
} |
| 191 |
|
| 192 |
\newcommand{\addviolinplot}[2][]{% |
| 193 |
\pgfkeys{/violinplot, #1} |
| 194 |
|
| 195 |
\edef\violin@filename{#2} |
| 196 |
|
| 197 |
\pgfkeys{/pgf/fpu} |
| 198 |
|
| 199 |
\pgfplotstableread[% |
| 200 |
col sep=\violin@colsep, |
| 201 |
trim cells=true, |
| 202 |
]{\violin@filename}\theviolin@dataset@table |
| 203 |
|
| 204 |
\pgfplotstablegetrowsof{\theviolin@dataset@table} |
| 205 |
\pgfmathparse{int(\pgfmathresult-1)} |
| 206 |
\let\violin@filename@lastindex\pgfmathresult |
| 207 |
|
| 208 |
\pgfmathqparse{0} |
| 209 |
\let\violin@dataset@stddev\pgfmathresult |
| 210 |
\let\violin@dataset@average\pgfmathresult |
| 211 |
\let\violin@dataset@weightsum\pgfmathresult |
| 212 |
\let\violin@weight@row\pgfmathresult |
| 213 |
|
| 214 |
% Calculating averages |
| 215 |
\def\violin@frequencies@default{FREQINTERNALVIOLIN} |
| 216 |
\pgfplotstableforeachcolumnelement{\violin@index}\of\theviolin@dataset@table\as\xi{% |
| 217 |
\ifx\violin@frequencies\violin@frequencies@default |
| 218 |
\pgfmathqparse{1} |
| 219 |
\let\violin@dataset@weight\pgfmathresult |
| 220 |
\else |
| 221 |
\pgfplotstablegetelem{\violin@weight@row}{\violin@frequencies}\of{\theviolin@dataset@table} |
| 222 |
\let\violin@dataset@weight\pgfplotsretval |
| 223 |
\pgfmathparse{\violin@weight@row+1} |
| 224 |
\let\violin@weight@row\pgfmathresult |
| 225 |
\fi |
| 226 |
\pgfmathparse{\violin@dataset@weightsum+\violin@dataset@weight} |
| 227 |
\let\violin@dataset@weightsum\pgfmathresult |
| 228 |
\pgfmathparse{\violin@dataset@average+\xi*\violin@dataset@weight} |
| 229 |
\let\violin@dataset@average\pgfmathresult |
| 230 |
} |
| 231 |
\pgfmathparse{\violin@dataset@average/\violin@dataset@weightsum} |
| 232 |
\let\violin@dataset@average\pgfmathresult |
| 233 |
|
| 234 |
\ifviolin@datapoints |
| 235 |
\pgfplotstablecreatecol[ |
| 236 |
create col/assign/.code={ |
| 237 |
\let\entry\violin@delta |
| 238 |
\pgfkeyslet{/pgfplots/table/create col/next content}\entry |
| 239 |
} |
| 240 |
]{deltacol}\theviolin@dataset@table |
| 241 |
\fi |
| 242 |
|
| 243 |
\ifviolin@reverseaxis |
| 244 |
\def\violin@axis@x{y} |
| 245 |
\def\violin@axis@y{x} |
| 246 |
\else |
| 247 |
\def\violin@axis@x{x} |
| 248 |
\def\violin@axis@y{y} |
| 249 |
\fi |
| 250 |
|
| 251 |
% Bandwidth calculation |
| 252 |
\def\violin@bandwidth@default{NONE} |
| 253 |
\ifx\violin@bandwidth\violin@bandwidth@default |
| 254 |
\pgfmathqparse{0} |
| 255 |
\let\violin@weight@row\pgfmathresult |
| 256 |
% Standard deviation calculation |
| 257 |
\pgfplotstableforeachcolumnelement{\violin@index}\of\theviolin@dataset@table\as\xi{% |
| 258 |
\ifx\violin@frequencies\violin@frequencies@default |
| 259 |
\pgfmathqparse{1} |
| 260 |
\let\violin@dataset@weight\pgfmathresult |
| 261 |
\else |
| 262 |
\pgfplotstablegetelem{\violin@weight@row}{\violin@frequencies}\of{\theviolin@dataset@table} |
| 263 |
\let\violin@dataset@weight\pgfplotsretval |
| 264 |
\pgfmathparse{\violin@weight@row+1} |
| 265 |
\let\violin@weight@row\pgfmathresult |
| 266 |
\fi |
| 267 |
\pgfmathparse{\violin@dataset@stddev+\violin@dataset@weight*(\xi-\violin@dataset@average)^2} |
| 268 |
\let\violin@dataset@stddev\pgfmathresult |
| 269 |
} |
| 270 |
\pgfmathparse{sqrt(\violin@dataset@stddev/\violin@dataset@weightsum)} |
| 271 |
\let\violin@dataset@stddev\pgfmathresult |
| 272 |
\pgfmathparse{1.06*\violin@dataset@stddev*(\violin@dataset@weightsum^(-0.2))}% |
| 273 |
\let\violin@bandwidthcalc\pgfmathresult |
| 274 |
\else |
| 275 |
\let\violin@bandwidthcalc\violin@bandwidth |
| 276 |
\fi |
| 277 |
|
| 278 |
% Setting limits for curve |
| 279 |
\pgfplotstablegetelem{0}{\violin@index}\of{\theviolin@dataset@table} |
| 280 |
\let\violin@dataset@min\pgfplotsretval |
| 281 |
\let\violin@dataset@max\pgfplotsretval |
| 282 |
\pgfplotstableforeachcolumnelement{\violin@index}\of\theviolin@dataset@table\as\xi{% |
| 283 |
\pgfmathparse{\xi > \violin@dataset@min} |
| 284 |
\pgfmathfloattoint{\pgfmathresult} |
| 285 |
\ifnum\pgfmathresult=0 |
| 286 |
\let\violin@dataset@min\xi |
| 287 |
\fi |
| 288 |
} |
| 289 |
\pgfmathparse{\violin@dataset@min - 3*\violin@bandwidthcalc} |
| 290 |
\let\violin@dataset@min\pgfmathresult |
| 291 |
|
| 292 |
\pgfplotstableforeachcolumnelement{\violin@index}\of\theviolin@dataset@table\as\xi{% |
| 293 |
\pgfmathparse{\xi < \violin@dataset@max} |
| 294 |
\pgfmathfloattoint{\pgfmathresult} |
| 295 |
\ifnum\pgfmathresult=0 |
| 296 |
\let\violin@dataset@max\xi |
| 297 |
\fi |
| 298 |
} |
| 299 |
\pgfmathparse{\violin@dataset@max + 3*\violin@bandwidthcalc} |
| 300 |
\let\violin@dataset@max\pgfmathresult |
| 301 |
|
| 302 |
% Creating curve domain |
| 303 |
\pgfplotstableset{ |
| 304 |
create on use/list/.style={create col/expr={ |
| 305 |
\violin@dataset@min + (\violin@dataset@max-\violin@dataset@min) |
| 306 |
*(\pgfplotstablerow/(\violin@samples-1)) |
| 307 |
}} |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
% Calculating KDE |
| 312 |
\pgfplotstablenew[columns={list}]{\violin@samples}\violin@kde |
| 313 |
|
| 314 |
\pgfmathqparse{0} |
| 315 |
\let\violin@kde@max\pgfmathresult |
| 316 |
|
| 317 |
\def\violin@kernel@gaussian{gaussian} |
| 318 |
\def\violin@kernel@logistic{logistic} |
| 319 |
\def\violin@kernel@parabolic{parabolic} |
| 320 |
\def\violin@kernel@uniform{uniform} |
| 321 |
\def\violin@kernel@triangular{triangular} |
| 322 |
|
| 323 |
\pgfplotstablecreatecol[ |
| 324 |
create col/assign/.code={ |
| 325 |
\pgfmathparse{% |
| 326 |
1.0/(\violin@dataset@weightsum*\violin@bandwidthcalc) |
| 327 |
} |
| 328 |
\let\violin@kde@factor\pgfmathresult |
| 329 |
\pgfmathqparse{0} |
| 330 |
\let\violin@kde@accum\pgfmathresult |
| 331 |
\let\violin@weight@row\pgfmathresult |
| 332 |
\pgfplotstableforeachcolumnelement{\violin@index}\of\theviolin@dataset@table\as\xi{% |
| 333 |
\ifx\violin@frequencies\violin@frequencies@default |
| 334 |
\pgfmathqparse{1} |
| 335 |
\let\violin@dataset@weight\pgfmathresult |
| 336 |
\else |
| 337 |
\pgfplotstablegetelem{\violin@weight@row}{\violin@frequencies}\of{\theviolin@dataset@table} |
| 338 |
\let\violin@dataset@weight\pgfplotsretval |
| 339 |
\pgfmathparse{\violin@weight@row+1} |
| 340 |
\let\violin@weight@row\pgfmathresult |
| 341 |
\fi |
| 342 |
\pgfmathparse{% |
| 343 |
((\xi-\thisrow{list})/% |
| 344 |
\violin@bandwidthcalc)} |
| 345 |
\let\violin@u\pgfmathresult |
| 346 |
% |
| 347 |
\ifx\violin@kernel\violin@kernel@gaussian |
| 348 |
\pgfmathparse{\violin@kde@accum + % |
| 349 |
\violin@dataset@weight*e^(-0.5*\violin@u*\violin@u)/sqrt(2*pi)} |
| 350 |
\let\violin@kde@accum\pgfmathresult |
| 351 |
\fi |
| 352 |
\ifx\violin@kernel\violin@kernel@logistic |
| 353 |
\pgfmathparse{\violin@kde@accum + % |
| 354 |
\violin@dataset@weight*1/(e^\violin@u + 2 + e^(-\violin@u)) |
| 355 |
} |
| 356 |
\let\violin@kde@accum\pgfmathresult |
| 357 |
\fi |
| 358 |
\ifx\violin@kernel\violin@kernel@parabolic |
| 359 |
\pgfmathparse{abs(\violin@u) < 1} |
| 360 |
\pgfmathfloattoint{\pgfmathresult} |
| 361 |
\ifnum\pgfmathresult=1 |
| 362 |
\pgfmathparse{\violin@kde@accum + % |
| 363 |
\violin@dataset@weight*0.75*(1-\violin@u*\violin@u) |
| 364 |
} |
| 365 |
\else |
| 366 |
\pgfmathparse{\violin@kde@accum} |
| 367 |
\fi |
| 368 |
\let\violin@kde@accum\pgfmathresult |
| 369 |
\fi |
| 370 |
\ifx\violin@kernel\violin@kernel@uniform |
| 371 |
\pgfmathparse{abs(\violin@u) < 1} |
| 372 |
\pgfmathfloattoint{\pgfmathresult} |
| 373 |
\ifnum\pgfmathresult=1 |
| 374 |
\pgfmathparse{\violin@kde@accum + \violin@dataset@weight*0.5} |
| 375 |
\else |
| 376 |
\pgfmathparse{\violin@kde@accum} |
| 377 |
\fi |
| 378 |
\let\violin@kde@accum\pgfmathresult |
| 379 |
\fi |
| 380 |
\ifx\violin@kernel\violin@kernel@triangular |
| 381 |
\pgfmathparse{abs(\violin@u) < 1} |
| 382 |
\pgfmathfloattoint{\pgfmathresult} |
| 383 |
\ifnum\pgfmathresult=1 |
| 384 |
\pgfmathparse{\violin@kde@accum + % |
| 385 |
\violin@dataset@weight*(1-abs(\violin@u))} |
| 386 |
\else |
| 387 |
\pgfmathparse{\violin@kde@accum} |
| 388 |
\fi |
| 389 |
\let\violin@kde@accum\pgfmathresult |
| 390 |
\fi |
| 391 |
} |
| 392 |
\pgfmathparse{\violin@kde@accum*\violin@kde@factor} |
| 393 |
\let\entry\pgfmathresult |
| 394 |
\pgfkeyslet{/pgfplots/table/create col/next content}\entry |
| 395 |
} |
| 396 |
]{kdecol}\violin@kde |
| 397 |
|
| 398 |
\pgfplotstableforeachcolumnelement{kdecol}\of\violin@kde\as\entry{% |
| 399 |
\pgfmathparse{\entry < \violin@kde@max} |
| 400 |
\pgfmathfloattoint{\pgfmathresult} |
| 401 |
\ifnum\pgfmathresult=0 |
| 402 |
\let\violin@kde@max\entry |
| 403 |
\fi |
| 404 |
} |
| 405 |
|
| 406 |
\pgfplotstablemodifyeachcolumnelement{kdecol}\of\violin@kde\as\cell{% |
| 407 |
\let\pgfplotstablezero\violin@delta |
| 408 |
\ifnum\pgfplotstablerow=0 |
| 409 |
\let\cell\pgfplotstablezero |
| 410 |
\else |
| 411 |
\pgfmathparse{int(\violin@samples-1)} |
| 412 |
\pgfmathfloattoint{\pgfmathresult} |
| 413 |
\ifnum\pgfplotstablerow=\pgfmathresult |
| 414 |
\let\cell\pgfplotstablezero |
| 415 |
\else |
| 416 |
\ifviolin@nomirror |
| 417 |
\pgfmathqparse{1.9} |
| 418 |
\else |
| 419 |
\pgfmathqparse{1} |
| 420 |
\fi |
| 421 |
\let\violin@mirror@factor\pgfmathresult |
| 422 |
\ifviolin@scaled |
| 423 |
\pgfmathparse{\violin@delta+\violin@mirror@factor*0.5*\cell/\violin@kde@max} |
| 424 |
\else |
| 425 |
\pgfmathparse{\violin@delta+\violin@mirror@factor*\cell} |
| 426 |
\fi |
| 427 |
\let\cell\pgfmathresult |
| 428 |
\fi |
| 429 |
\fi |
| 430 |
} |
| 431 |
|
| 432 |
\ifviolin@nomirror |
| 433 |
\ifviolin@invert |
| 434 |
\pgfplotstablemodifyeachcolumnelement{kdecol}\of\violin@kde\as\cell{% |
| 435 |
\pgfmathparse{-\cell+2*\violin@delta} |
| 436 |
\let\cell\pgfmathresult |
| 437 |
} |
| 438 |
\fi |
| 439 |
\else |
| 440 |
\pgfplotstablecreatecol[ |
| 441 |
create col/assign/.code={ |
| 442 |
\pgfmathparse{-\thisrow{kdecol}+2*\violin@delta} |
| 443 |
\let\entry\pgfmathresult |
| 444 |
\pgfkeyslet{/pgfplots/table/create col/next content}\entry |
| 445 |
} |
| 446 |
]{kdecolmirror}\violin@kde |
| 447 |
|
| 448 |
\pgfplotstablenew[columns={list}]{\violin@samples}\violin@kdewithmirror |
| 449 |
\pgfplotstablecreatecol[ |
| 450 |
copy column from table={\violin@kde}{kdecol} |
| 451 |
]{kdecol}{\violin@kdewithmirror} |
| 452 |
\pgfplotstablesort[ |
| 453 |
sort key=list, |
| 454 |
sort cmp=float <, |
| 455 |
]{\violin@kdewithmirror}{\violin@kdewithmirror} |
| 456 |
|
| 457 |
\pgfplotstablenew[columns={list}]{\violin@samples}\violin@kdemirror |
| 458 |
\pgfplotstablecreatecol[ |
| 459 |
copy column from table={\violin@kde}{kdecolmirror} |
| 460 |
]{kdecol}{\violin@kdemirror} |
| 461 |
\pgfplotstablesort[ |
| 462 |
sort key=list, |
| 463 |
sort cmp=float >, |
| 464 |
]{\violin@kdemirror}{\violin@kdemirror} |
| 465 |
|
| 466 |
\pgfplotstablevertcat{\violin@kdewithmirror}{\violin@kdemirror} |
| 467 |
\fi |
| 468 |
|
| 469 |
\pgfkeys{/pgf/fpu=false} |
| 470 |
|
| 471 |
% Data points must be centered only if plot is mirrored |
| 472 |
\ifviolin@nomirror |
| 473 |
\ifviolin@invert |
| 474 |
\def\randfct{(-rnd)} |
| 475 |
\else |
| 476 |
\def\randfct{(rnd)} |
| 477 |
\fi |
| 478 |
\else |
| 479 |
\def\randfct{(rand)} |
| 480 |
\fi |
| 481 |
|
| 482 |
\ifviolin@nomirror |
| 483 |
\ifviolin@reverseaxis |
| 484 |
\edef\violin@addplot{ |
| 485 |
\noexpand\addplot[ |
| 486 |
no marks, |
| 487 |
fill=\violin@color, |
| 488 |
fill opacity=\violin@fillopacity, |
| 489 |
] table [ |
| 490 |
\violin@axis@x=kdecol, \violin@axis@y=list |
| 491 |
] {\noexpand\violin@kde}; |
| 492 |
} |
| 493 |
\violin@addplot |
| 494 |
\edef\violin@addplot{ |
| 495 |
\noexpand\addplot[ |
| 496 |
no marks, |
| 497 |
color=black, |
| 498 |
] coordinates { |
| 499 |
(\violin@xmin,\violin@delta)% |
| 500 |
(\violin@xmax,\violin@delta) |
| 501 |
}; |
| 502 |
} |
| 503 |
\violin@addplot |
| 504 |
\else |
| 505 |
\edef\violin@addplot{ |
| 506 |
\noexpand\addplot[ |
| 507 |
no marks, |
| 508 |
fill=\violin@color, |
| 509 |
fill opacity=\violin@fillopacity, |
| 510 |
] table [ |
| 511 |
\violin@axis@x=kdecol, \violin@axis@y=list |
| 512 |
] {\noexpand\violin@kde}; |
| 513 |
} |
| 514 |
\violin@addplot |
| 515 |
\edef\violin@addplot{ |
| 516 |
\noexpand\addplot[ |
| 517 |
no marks, |
| 518 |
color=black, |
| 519 |
] coordinates { |
| 520 |
(\violin@delta,\violin@ymin)% |
| 521 |
(\violin@delta,\violin@ymax) |
| 522 |
}; |
| 523 |
} |
| 524 |
\violin@addplot |
| 525 |
\fi |
| 526 |
\else |
| 527 |
\edef\violin@addplot{ |
| 528 |
\noexpand\addplot[ |
| 529 |
no marks, |
| 530 |
fill=\violin@color, |
| 531 |
fill opacity=\violin@fillopacity, |
| 532 |
] table [ |
| 533 |
\violin@axis@x=kdecol, \violin@axis@y=list |
| 534 |
] {\noexpand\violin@kdewithmirror}; |
| 535 |
} |
| 536 |
\violin@addplot |
| 537 |
\fi |
| 538 |
|
| 539 |
\ifviolin@averages |
| 540 |
\ifviolin@reverseaxis |
| 541 |
\edef\violin@addplot{ |
| 542 |
\noexpand\addplot[ |
| 543 |
only marks, |
| 544 |
mark=\violin@avg@mark, |
| 545 |
mark size=\violin@avg@size, |
| 546 |
color=\violin@avg@color, |
| 547 |
opacity=\violin@avg@opacity, |
| 548 |
fill=\violin@avg@fillcolor, |
| 549 |
fill opacity=\violin@avg@fillopacity, |
| 550 |
] coordinates { |
| 551 |
(\violin@dataset@average,\violin@delta) |
| 552 |
}; |
| 553 |
} |
| 554 |
\violin@addplot |
| 555 |
\else |
| 556 |
\edef\violin@addplot{ |
| 557 |
\noexpand\addplot[ |
| 558 |
only marks, |
| 559 |
mark=\violin@avg@mark, |
| 560 |
mark size=\violin@avg@size, |
| 561 |
color=\violin@avg@color, |
| 562 |
opacity=\violin@avg@opacity, |
| 563 |
fill=\violin@avg@fillcolor, |
| 564 |
fill opacity=\violin@avg@fillopacity, |
| 565 |
] coordinates { |
| 566 |
(\violin@delta,\violin@dataset@average) |
| 567 |
}; |
| 568 |
} |
| 569 |
\violin@addplot |
| 570 |
\fi |
| 571 |
\fi |
| 572 |
|
| 573 |
\ifviolin@datapoints |
| 574 |
\edef\violin@addplot{ |
| 575 |
\noexpand\addplot[ |
| 576 |
only marks, |
| 577 |
mark=\violin@pts@mark, |
| 578 |
mark size=\violin@pts@size, |
| 579 |
color=\violin@pts@color, |
| 580 |
opacity=\violin@pts@opacity, |
| 581 |
fill=\violin@pts@fillcolor, |
| 582 |
fill opacity=\violin@pts@fillopacity, |
| 583 |
\violin@axis@x\space |
| 584 |
filter/.expression={\violin@axis@x + \violin@pts@jitter * \randfct} |
| 585 |
] table [ |
| 586 |
\violin@axis@x=deltacol, |
| 587 |
\violin@axis@y=\violin@index, |
| 588 |
] {\noexpand\theviolin@dataset@table}; |
| 589 |
} |
| 590 |
\violin@addplot |
| 591 |
\fi |
| 592 |
} |
| 593 |
|
| 594 |
\newcommand{\violin@getnth}[2]{% |
| 595 |
\pgfmathqparse{0} |
| 596 |
\let\violin@getnth@counter\pgfmathresult |
| 597 |
\let\violin@nthelem\pgfmathresult |
| 598 |
\@for\violin@listelem:=#1\do{ |
| 599 |
\ifnum#2=\violin@getnth@counter |
| 600 |
\let\violin@nthelem\violin@listelem |
| 601 |
\pgfmathparse{int(\violin@getnth@counter+1)} |
| 602 |
\let\violin@getnth@counter\pgfmathresult |
| 603 |
\else |
| 604 |
\pgfmathparse{int(\violin@getnth@counter+1)} |
| 605 |
\let\violin@getnth@counter\pgfmathresult |
| 606 |
\fi |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
\newcommand{\violinplot}[2][]{% |
| 611 |
\pgfkeys{/violinplot, default} |
| 612 |
\pgfkeysalsofrom{\violin@args} |
| 613 |
\pgfkeys{/violinplot, #1} |
| 614 |
|
| 615 |
\def\violin@filename{#2} |
| 616 |
|
| 617 |
\ifviolin@reverseaxis |
| 618 |
\pgfplotsset{ |
| 619 |
yticklabels/.expand once={\violin@label}, |
| 620 |
ytick/.expand once={\violin@delta}, |
| 621 |
yticklabel style={\violin@datalabelstyle}, |
| 622 |
} |
| 623 |
\else |
| 624 |
\pgfplotsset{ |
| 625 |
xticklabels/.expand once={\violin@label}, |
| 626 |
xtick/.expand once={\violin@delta}, |
| 627 |
xticklabel style={\violin@datalabelstyle}, |
| 628 |
} |
| 629 |
\fi |
| 630 |
|
| 631 |
\pgfmathparse{\violin@numofplots!=0} % Current plot is not first plot |
| 632 |
\ifnum\pgfmathresult=1 |
| 633 |
\ifviolin@reverseaxis |
| 634 |
\pgfplotsset{hide x axis} |
| 635 |
\else |
| 636 |
\pgfplotsset{hide y axis} |
| 637 |
\fi |
| 638 |
|
| 639 |
\pgfplotsset{ |
| 640 |
title={}, |
| 641 |
xlabel={}, |
| 642 |
ylabel={}, |
| 643 |
axis line style={draw=none}, |
| 644 |
grid={none}, |
| 645 |
} |
| 646 |
\fi |
| 647 |
|
| 648 |
\begin{axis}[ |
| 649 |
axis on top, |
| 650 |
] |
| 651 |
\addviolinplot{\violin@filename} |
| 652 |
\end{axis} |
| 653 |
|
| 654 |
\pgfmathparse{int(\violin@numofplots+1)} |
| 655 |
\let\violin@numofplots\pgfmathresult |
| 656 |
} |
| 657 |
|
| 658 |
\newcommand{\violinplotwholefile}[2][]{% |
| 659 |
\pgfkeys{/violinplot, default} |
| 660 |
\pgfkeysalsofrom{\violin@args} |
| 661 |
\pgfkeys{/violinplot, #1} |
| 662 |
|
| 663 |
\def\violin@filename{#2} |
| 664 |
|
| 665 |
\pgfmathqparse{0} |
| 666 |
\let\violin@numofplots\pgfmathresult |
| 667 |
\@for\violin@plotindex:=\violin@indexes\do{ |
| 668 |
\pgfmathparse{int(\violin@numofplots+1)} |
| 669 |
\let\violin@numofplots\pgfmathresult |
| 670 |
} |
| 671 |
|
| 672 |
\pgfmathqparse{0} |
| 673 |
\let\violin@counter\pgfmathresult |
| 674 |
\let\violin@relativepos\pgfmathresult |
| 675 |
|
| 676 |
\pgfmathqparse{1} |
| 677 |
\let\violin@start\pgfmathresult |
| 678 |
\pgfmathparse{1+\violin@spacing} |
| 679 |
\let\violin@step\pgfmathresult |
| 680 |
\pgfmathparse{\violin@spacing*(\violin@numofplots-\violin@start)+\violin@start} |
| 681 |
\let\violin@stop\pgfmathresult |
| 682 |
|
| 683 |
\ifviolin@reverseaxis |
| 684 |
\pgfplotsset{ |
| 685 |
yticklabels/.expand once={\violin@labels}, |
| 686 |
ytick/.expand once={\violin@start, \violin@step, ..., \violin@stop}, |
| 687 |
yticklabel style={\violin@datalabelstyle}, |
| 688 |
} |
| 689 |
\else |
| 690 |
\pgfplotsset{ |
| 691 |
xticklabels/.expand once={\violin@labels}, |
| 692 |
xtick/.expand once={\violin@start, \violin@step, ..., \violin@stop}, |
| 693 |
xticklabel style={\violin@datalabelstyle}, |
| 694 |
} |
| 695 |
\fi |
| 696 |
|
| 697 |
\begin{axis}[ |
| 698 |
axis on top, |
| 699 |
] |
| 700 |
\@for\violin@plotindex:=\violin@indexes\do{ |
| 701 |
\ifnum\violin@numofplots=1 |
| 702 |
\pgfmathqparse{50} |
| 703 |
\else |
| 704 |
\pgfmathparse{int(\violin@counter*(100/(\violin@numofplots-1)))} |
| 705 |
\fi |
| 706 |
\let\violin@color@deg\pgfmathresult |
| 707 |
|
| 708 |
\pgfmathparse{1+\violin@counter*\violin@spacing} |
| 709 |
\let\violin@relativepos\pgfmathresult |
| 710 |
|
| 711 |
\addviolinplot[% |
| 712 |
index=\violin@plotindex, |
| 713 |
relative position=\violin@relativepos, |
| 714 |
color={\violin@color@primary!\violin@color@deg!\violin@color@secondary}, |
| 715 |
]{\violin@filename} |
| 716 |
|
| 717 |
\pgfmathparse{int(\violin@counter+1)} |
| 718 |
\let\violin@counter\pgfmathresult |
| 719 |
} |
| 720 |
\end{axis} |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
% PGFplots-analogous alias |
| 725 |
\let\violinplotsset\violinsetoptions |
| 726 |
|