summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/tikzviolinplots/tikzviolinplots.tex
blob: e53191ffe7a027e5953756f5eef27a201be3cd59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
%% tikzviolinplots.tex
%% Copyright 2023 Pedro Callil-Soares
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Pedro Callil-Soares.
%
% This work consists of the files tikzviolinplots.sty and
% tikzviolinplots.tex.

\documentclass{article}

\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{width=\textwidth,compat=1.18}
\usepgfplotslibrary{statistics}
\usepackage{tikzviolinplots}
\usepgfplotslibrary{external}
\tikzexternalize
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.text}
\usetikzlibrary{decorations.pathmorphing}
\usepackage{minted}
\usemintedstyle{gruvbox-light}
\usepackage{scontents}
\usepackage{wasysym}
\usepackage{microtype}
\usepackage{subcaption}
\usepackage{hyperref}

\begin{scontents}[write-out=violinandboxplotexample.dat]
A	B	C	D
0.876	0.574	2.175	1.684
1.015	0.822	2.121	2.290
0.995	1.266	1.916	2.184
1.101	0.740	2.044	2.065
1.222	0.636	2.004	2.248
0.712	1.753	2.038	2.200
1.063	0.873	1.974	1.865
0.973	1.020	2.145	1.895
1.174	0.763	2.052	2.240
1.057	1.293	2.079	2.360
1.004	0.853	2.064	1.695
0.909	0.694	2.136	1.837
1.108	0.849	2.000	1.744
1.210	0.468	2.010	1.820
0.958	0.767	1.824	2.158
1.119	0.940	2.034	1.846
1.000	0.962	1.967	2.545
0.954	1.551	1.833	1.816
0.918	0.755	1.914	2.369
1.093	0.973	1.997	1.543
\end{scontents}

\begin{scontents}[write-out=example.dat]
A	B	C	D	E
0.3	-2.1	3.50	2.89	1.00
0.41	-1.9	3.55	2.88	1.06
0.45	-1.5	3.55	3.13	1.00
0.46	-1.3	3.60	2.69	1.20
0.46	-1.3	3.60	2.78	1.00
0.46	-1.27	3.60	2.83	1.35
0.47	-1.26	3.65	3.08	1.00
0.47	-1.26	3.65	3.08	1.53
0.48	-1.24	3.65	2.73	1.00
0.51	-1.2	3.65	3.08	1.73
0.57	-1.13	3.65	3.24	1.00
2.3	-1.02	3.70	3.10	1.95
2.41	-0.9	3.70	2.98	1.00
2.46	-0.2	3.70	2.98	2.21
2.47	0.0	3.75	3.04	1.00
2.48	0.1	3.80	3.24	2.49
2.51	0.3	3.85	3.16	1.00
2.57	0.5	3.85	3.30	3.04
\end{scontents}

\title{The \texttt{tikzviolinplots} package}
\author{Pedro Callil-Soares}
\date{\today}

\begin{document}

\maketitle

\tableofcontents

\begin{abstract}
	The package provides commands for violin plot creation and
	the kernel density estimations required.
\end{abstract}

\section{Introduction}

This package, through the use of the package \texttt{pgfplots}, allows the
creation of violin plots in \LaTeX. Violin plots are similar to boxplots,
but instead of a box signalling the average and quartiles, a kernel density
estimator is plotted, as in equation \ref{eq:kde}, in which the function $k$
(the kernel) is a probability distribution, the positive number $h$ (the
bandwidth) is a smoothing factor and $n$ is the sample size.

\begin{equation}
	\label{eq:kde}
	\textnormal{KDE}(x) = %
	\cfrac{1}{nh}\sum_{i=1}^nk\left(\cfrac{x-x_i}{h}\right)
\end{equation}

A comparison between the two plots, showcasing its similarities, is
shown in figures \ref{fig:example}. The violin plot in figure
\ref{graph:violin_example} assumes normal data, and the bandwidth
(smoothing factor $h$ in equation \ref{eq:kde}) is defined accordingly.

\pgfplotsset{height=1.6\linewidth}

\begin{figure}[h!]
	\centering
	\begin{subfigure}{0.5\textwidth}
		\centering
		\begin{tikzpicture}
			\begin{axis}
				[
					boxplot/draw direction=y,
					ymax=3, ymin=0,
					xmin=0, xmax=5,
					ymajorgrids=true,
					xtick={1,2,3,4},
					xticklabels={$\alpha$,$\beta$,$\gamma$,$\delta$},
					ylabel={Some property},
				]
				\addplot+[boxplot,
					blue!100!red,
					fill=blue!100!red,
					fill opacity=0.50,
					no marks]
					table [y=A] {violinandboxplotexample.dat};
				\addplot+[boxplot,
					blue!66!red,
					fill=blue!66!red,
					fill opacity=0.50,
					no marks]
					table [y=B] {violinandboxplotexample.dat};
				\addplot+[boxplot,
					blue!33!red,
					fill=blue!33!red,
					fill opacity=0.50,
					no marks]
					table [y=C] {violinandboxplotexample.dat};
				\addplot+[boxplot,
					blue!0!red,
					fill=blue!0!red,
					fill opacity=0.50,
					no marks]
					table [y=D] {violinandboxplotexample.dat};
			\end{axis}
		\end{tikzpicture}
		\caption{Box plot}
		\label{graph:box_example}
	\end{subfigure}%
	\hfill%
	\begin{subfigure}{0.5\textwidth}
		\centering
		\begin{tikzpicture}
			\violinsetoptions[
				averages,
				data points,
				scaled,
			]{
				xmin=0,xmax=5,
				ymin=0,ymax=3,
				xlabel style={
					yshift = {-2*height("a")}
				},
				ymajorgrids=true,
				ylabel={Same property},
			}
			\violinplotwholefile[%
				primary color=red,
				secondary color=blue,
				indexes={A,B,C,D},
				spacing=1.0,
				labels={%
					$\alpha$,
					$\beta$,
					$\gamma$,
					$\delta$,
				},
				col sep=tab,
				dataset size=1pt,
				dataset mark=*,
				dataset fill=black!50!white,
				dataset fill opacity=1.0,
				average mark=x,
				average size=5pt,
			]{violinandboxplotexample.dat}
		\end{tikzpicture}
		\caption{Violin plot}
		\label{graph:violin_example}
	\end{subfigure}
	\caption{Box and violin plot examples}
	\label{fig:example}
\end{figure}

\pgfplotsset{height=0.9\linewidth}

\section{Usage}

To plot a violin plot with the commands provided, one must, inside
a \texttt{tikzpicture} environment, set the general options to all
plots and insert each individual dataset.

To set the general options, the command \texttt{{\textbackslash}violinsetoptions}
is provided, and must be invoked before plotting the datasets. This should
be done with the commands \texttt{{\textbackslash}violinplot} or
\texttt{{\textbackslash}violinplotwholefile}.

\subsection{General options: \texttt{{\textbackslash}violinsetoptions}}

The command \texttt{{\textbackslash}violinsetoptions} takes two arguments,
an optional argument with package-specific options and a mandatory
argument with options to be passed to \texttt{pgfplots}.

\begin{minted}[escapeinside=||]{latex}
	\violinsetoptions[|\textit{<package-specific options>}|]%
		{|\textit{<pgfplots general options>}|}
\end{minted}

\subsubsection{Package-specific options}

There are five options specific to the package: \texttt{scaled},
\texttt{data points}, \texttt{averages}, \texttt{no mirror} and
\texttt{reverse axis}, controlling how and which information
in the datasets should be presented.

The option \texttt{scaled} controls if all plots in the graph
have the same area or same width. If passed, the kernel
distribution estimations will be scaled to the same width, as
shown in figure \ref{graph:violin_verti}; otherwise, the plots
will present the same area, as in figure \ref{graph:violin_horiz}.

The option \texttt{data points}, if passed, will show, along with
the violin plots, the distribution of points in the data set, as shown in
figure \ref{graph:violin_verti}.

If the option \texttt{averages} is passed, the average of the data set
elements is shown, as in figure \ref{graph:violin_horiz}.

The plots are mirrored by default; however, passing the option \texttt{no mirror}
will show only half the plot, as shown in figure \ref{graph:violin_horiz}.

Finally, to ``transpose'' the plots (\textit{i.e.} show the distributions
as functions of the abcissa, as in figure \ref{graph:violin_horiz},
and not as functions of ordinate, as in figure \ref{graph:violin_verti}),
one might use the option \texttt{reverse axis}.

\subsubsection{Plot limits and other \texttt{pgfplots} options}

The minima and maxima of the plot axes must be set in the second (and first
mandatory) argument to the command, and should follow \texttt{pgfplots}
syntax. For instance, to set the minimum and maximum of the $x$-axis
to -3 and 6, and of the $y$-axis to 2.5 and 7, one might use:

\begin{minted}[escapeinside=||]{latex}
	\violinsetoptions[|\textit{<package-specific options>}|]%
		{xmin=-3, xmax=6, ymin=2.5, xmin=7,%
		|\textit{<pgfplots general options>}|}
\end{minted}

Other \texttt{pgfplots} expressions such as title or axes labels may be set
in the same way in this argument.

\subsection{Options for each data set: \texttt{{\textbackslash}violinplot}}

If the data sets are not very similar and/or advanced customizations
are desired, \texttt{{\textbackslash}violinplot} should be used to
plot each data set individually.
This command takes one mandatory argument, and a list of options:

\begin{minted}[escapeinside=||]{latex}
	\violinplot[%
		|\textit{<option>}=\textit{<value>}|
	]{|\textit{filename}|}
\end{minted}

The filename (mandatory argument) must be a path to a file storing the data as
space-separated columns. The optional argument is a list of options, including:

\begin{itemize}
	\item \texttt{col sep}: Column separation character in filename.
		Defaults to \texttt{comma}, and can accept options
		\texttt{space}, \texttt{tab}, \texttt{comma},
		\texttt{colon}, \texttt{semicolon}, \texttt{braces},
		\texttt{\&} and \texttt{ampersand}.
	\item \texttt{index}: Necessary option, is the name of the column with
		the data to be plotted.
	\item \texttt{kernel}: The function to be used for the kernel density
		estimation; available values are \texttt{gaussian} (default),
		\texttt{logistic}, \texttt{parabolic}, \texttt{uniform}
		and \texttt{triangular}.
	\item \texttt{bandwidth}: Smoothing parameter for the kernel density
		estimation; defaults to $h$ in equation \ref{eq:band},
		which assumes gaussian distribution.
		\begin{equation}
			\label{eq:band}
			h=\sqrt[5]{\frac{4\times\textnormal{stddev}%
			\left(x_1,x_2,\ldots,x_n\right)^5}{3n}}
		\end{equation}
	\item \texttt{samples}: Number of points in which the kernel density
		estimation will be calculated and plotted. Defaults
		to 50.
	\item \texttt{relative position}: position of the plot in the axis
		with the labels for each dataset. Defaults to 0.
	\item \texttt{color}: Color of the plot, border and interior.
		Defaults to black. The interior is colored with 0.5
		opacity.
	\item \texttt{label}: Label of the plot, positioned at the point
		set by \texttt{relative position}.
	\item \texttt{invert}: If \texttt{true}, plot will be drawn in opposite
		side. Useful for comparing two parts of a data set, by plotting each
		group side by side.
	\item Options for average and data points:
	\begin{itemize}
		\item \texttt{average mark}, \texttt{dataset mark}: Mark used
			for averages and points of the data set (defaults
			to ``\texttt{x}'' and ``\texttt{*}'', respectively).
		\item \texttt{average size}, \texttt{dataset size}: Size
			of each mark (defaults to 3pt for averages and 1pt
			for other points).
		\item \texttt{average color}, \texttt{dataset color}: Color
			of each mark's border. Defaults to black in both
			cases.
		\item \texttt{average opacity}, \texttt{dataset opacity}:
			Border opacity. Defaults to 1.0 in both cases.
		\item \texttt{average fill}, \texttt{dataset fill}: Color
			of each mark's interior. Defaults to black for the
			points of the data set and white for the averages.
		\item \texttt{average fill opacity}, \texttt{dataset fill opacity}:
			Opacity of the marks. Defaults to 0.5 for averages and
			0.2 for the other points.
	\end{itemize}
\end{itemize}

\subsection{Simplified interface: \texttt{{\textbackslash}violinplotwholefile}}

If the data sets are similar and no customizations are required, one might
use the command \texttt{{\textbackslash}violinplotwholefile}.

\begin{minted}[escapeinside=||]{latex}
	\violinplotwholefile[%
		|\textit{<option>}=\textit{<value>}|
	]{|\textit{filename}|}
\end{minted}

This command will calculate and plot all columns named. The available options
are:

\begin{itemize}
	\item \texttt{primary color}: Primary color utilised. A gradient is built from
		the secondary color to it. Defaults to black.
	\item \texttt{secondary color}: Secondary color utilized. A
		gradient is built from it to the primary color. Defaults
		to white.
	\item \texttt{indexes}: List of columns to be plotted.
	\item \texttt{spacing}: Distance between plots. Defaults to 1.0.
	\item \texttt{labels}: Labels of the data to be plotted.
\end{itemize}

Besides, the options available to \texttt{{\textbackslash}violinplots} are also
available for this command; for instance, one might set the kernel to
be utilized as the \texttt{uniform} kernel through the option
\texttt{kernel=uniform}. Of course, \texttt{relative position} is not an option
here.

\section{Examples}

To show the usage of the package, we plotted several data sets two times, in
figures \ref{graph:violin_verti} and \ref{graph:violin_horiz}, using either the
simplified or the complete interface.

The data are in the file \texttt{example.dat}, shown below:

\colorlet{mintedbgcsv}{black!10!white}

\inputminted[%
	frame=single,
	rulecolor=mintedbgcsv,
	framesep=10pt,
	bgcolor=mintedbgcsv,
	obeytabs,
	tabsize=12,
]{text}{example.dat}

\subsection{Simplified Interface}

First we will plot this data using the simplified interface, resulting in
figure \ref{graph:violin_verti}.

\begin{figure}[h]
	\centering
	\begin{tikzpicture}
		\violinsetoptions[
			data points,
			scaled,
		]{
			xmin=0,xmax=6,
			ymin=-4,ymax=6,
			title={Plotting Some Data},
			xlabel={Groups},
			ylabel={Some property},
			xlabel style={
				yshift = {-2*height("a")}
			},
			ymajorgrids=true,
		}
		\violinplotwholefile[%
			primary color=orange,
			secondary color=black,
			indexes={C,B,A,D,E},
			spacing=1.0,
			labels={%
				$\alpha$,
				$\beta$,
				$\gamma$,
				$\delta$,
				$\epsilon$
			},
			col sep=tab,
			dataset size=2pt,
			dataset mark=diamond*,
			dataset fill=blue!50!white,
			dataset fill opacity=1.0,
		]{example.dat}
	\end{tikzpicture}
	\caption{Violin plot --- vertical example}
	\label{graph:violin_verti}
\end{figure}

Code for figure \ref{graph:violin_verti}, using
\texttt{{\textbackslash}violinplotwholefile}:

\colorlet{mintedbgverti}{orange!10!white}

\begin{minted}[%
	escapeinside=||,
	frame=single,
	rulecolor=mintedbgverti,
	framesep=10pt,
	tabsize=4,
	bgcolor=mintedbgverti]{latex}
\begin{tikzpicture}
	\violinsetoptions[
		data points,
		scaled,
	]{
		xmin=0,xmax=6,
		ymin=-4,ymax=6,
		title={Plotting Some Data},
		xlabel={Groups},
		ylabel={Some property},
		xlabel style={
			yshift = {-2*height("a")}
		},
		ymajorgrids=true,
	}
	\violinplotwholefile[%
		primary color=orange,
		secondary color=black,
		batch indexes={C,B,A,D,E},
		batch spacing=1.0,
		batch labels={%
			$\alpha$,
			$\beta$,
			$\gamma$,
			$\delta$,
			$\epsilon$
		},
		col sep=tab,
		dataset size=2pt,
		dataset mark=diamond*,
		dataset fill=blue!50!white,
		dataset fill opacity=1.0,
	]{example.dat}
\end{tikzpicture}

\end{minted}

\subsection{Complete Interface}

Then the data will be plotted using the complete interface, resulting
in figure \ref{graph:violin_horiz}.

\begin{figure}[h]
	\centering
	\begin{tikzpicture}
		\violinsetoptions[
			no mirror,
			averages,
			reverse axis,
		]{%
			xmin=-4,xmax=6,
			ymin=0.7,ymax=7,
			title={Plotting Some Data},
			ylabel={Groups},
			xlabel={Some property},
			ylabel style={
				yshift = {2*width("a")}
			},
			xmajorgrids=true,
		}
		\violinplot[%
			index=C,
			relative position=1,
			color=blue!0!black,
			label={$\alpha$},
			col sep=tab,
		]{example.dat}
		\violinplot[%
			index=B,
			relative position=2,
			color=blue!25!black,
			label={$\beta$},
			col sep=tab,
		]{example.dat}
		\violinplot[%
			index=A,
			relative position=3,
			color=blue!50!black,
			label={$\gamma$},
			col sep=tab,
			kernel=parabolic,
			samples=100,
			bandwidth=0.4,
			average mark=otimes*,
			average size=2pt,
			average fill=blue!70!black,
			average fill opacity=0.7,
		]{example.dat}
		\violinplot[%
			index=D,
			relative position=4,
			color=blue!75!black,
			label={$\delta$},
			col sep=tab,
		]{example.dat}
		\violinplot[%
			index=E,
			relative position=5,
			color=blue!100!black,
			label={$\epsilon$},
			col sep=tab,
		]{example.dat}
	\end{tikzpicture}
	\caption{Violin plot --- horizontal example}
	\label{graph:violin_horiz}
\end{figure}

Code for figure \ref{graph:violin_horiz}, using
\texttt{{\textbackslash}violinplot}:

\colorlet{mintedbghoriz}{blue!10!white}

\begin{minted}[%
	escapeinside=||,
	frame=single,
	rulecolor=mintedbghoriz,
	framesep=10pt,
	tabsize=4,
	bgcolor=mintedbghoriz]{latex}
\begin{tikzpicture}
	\violinsetoptions[
		no mirror,
		averages,
		reverse axis,
	]{%
		xmin=-4,xmax=6,
		ymin=0.7,ymax=7,
		title={Plotting Some Data},
		ylabel={Groups},
		xlabel={Some property},
		ylabel style={
			yshift = {2*width("a")}
		},
		xmajorgrids=true,
	}
	\violinplot[%
		index=C,
		relative position=1,
		color=blue!0!black,
		label={$\alpha$},
		col sep=tab,
	]{example.dat}
	\violinplot[%
		index=B,
		relative position=2,
		color=blue!25!black,
		label={$\beta$},
		col sep=tab,
	]{example.dat}
	\violinplot[%
		index=A,
		relative position=3,
		color=blue!50!black,
		label={$\gamma$},
		col sep=tab,
		kernel=parabolic,
		samples=100,
		bandwidth=0.4,
		average mark=otimes*,
		average size=2pt,
		average fill=blue!70!black,
		average fill opacity=0.7,
	]{example.dat};
	\violinplot[%
		index=D,
		relative position=4,
		color=blue!75!black,
		label={$\delta$},
		col sep=tab,
	]{example.dat}
	\violinplot[%
		index=E,
		relative position=5,
		color=blue!100!black,
		label={$\epsilon$},
		col sep=tab,
	]{example.dat}
\end{tikzpicture}
\end{minted}

\subsection{Drawings and Annotations}

As everything happens inside a \texttt{tikzpicture} environment, drawings
and annotations are straightforward, as seen in figure \ref{graph:violin_annotations}.

\begin{figure}[h]
	\centering
	\begin{tikzpicture}
		\violinsetoptions[
			averages,
			data points,
		]{
			xmin=0,xmax=6,
			ymin=-2,ymax=5,
			xlabel = {Bandwidth choice},
			ylabel = {Some property},
			title={Problems with Different Distributions},
			xlabel style={
				yshift = {-3*height("a")}
			},
			ymajorgrids=true,
		}
		\violinplot[%
			index=A,
			relative position=1,
			color=green!33!black,
			label={Estimated bandwidth},
			col sep=tab,
		]{example.dat}
		\violinplot[%
			index=A,
			bandwidth=0.1,
			relative position=4,
			color=green!12!black,
			label={Smaller bandwidth},
			col sep=tab,
		]{example.dat}
		\begin{axis}[
			xmin=0,xmax=6,
			ymin=-2,ymax=5,
		]
			\draw[-{Stealth}] (axis cs: 1.75,0.45) %
				.. controls (axis cs:1.75,0.85) and %
				(axis cs:3.9,0.5) .. (axis cs: 3.9,1.05);
			\draw[decoration={raise=2pt,text along path, text=%
				{|\tiny| Better bandwidth}, text align={center}}, %
				decorate] %
				(axis cs: 1.75,0.45) %
				.. controls (axis cs:1.75,0.85) and %
				(axis cs:3.9,0.5) .. (axis cs: 3.9,1.05);
			\draw[-{Stealth}] %
				(axis cs: 1.5,0.45) .. controls (axis cs:1.5,0.85) %
				and (axis cs:1.25,1.1) .. (axis cs: 1.25,1.5) .. %
				controls (axis cs:1.25,1.9) and (axis cs:1.35,1.9) %
				.. (axis cs:1.35,2.6);
			\draw[decoration={raise=2pt,text along path, text=%
				{|\tiny|Very oversmoothed}, text align={center}}, %
				decorate] %
				(axis cs: 1.35,2.6) .. controls (axis cs:1.35,1.9) %
				and (axis cs:1.25,1.9) .. (axis cs: 1.25,1.5) .. %
				controls (axis cs:1.25,1.1) and (axis cs:1.5,0.85) %
				.. (axis cs:1.5,0.45);
		\end{axis}
	\end{tikzpicture}
	\caption{Annotations in a plot}
	\label{graph:violin_annotations}
\end{figure}

Figure \ref{graph:violin_annotations} is compiled from the code excerpt below.

\colorlet{mintedbgannot}{green!10!white}

\begin{minted}[%
	escapeinside=||,
	frame=single,
	rulecolor=mintedbgannot,
	framesep=10pt,
	tabsize=4,
	bgcolor=mintedbgannot]{latex}
\begin{tikzpicture}
	\violinsetoptions[
		averages,
		data points,
	]{
		xmin=0,xmax=6,
		ymin=-2,ymax=5,
		xlabel = {Bandwidth choice},
		ylabel = {Some property},
		title={Problems with Different %
			Distributions},
		xlabel style={
			yshift = {-3*height("a")}
		},
		ymajorgrids=true,
	}
	\violinplot[%
		index=A,
		relative position=1,
		color=green!33!black,
		label={Estimated bandwidth},
		col sep=tab,
	]{example.dat}
	\violinplot[%
		index=A,
		bandwidth=0.1,
		relative position=4,
		color=green!12!black,
		label={Smaller bandwidth},
		col sep=tab,
	]{example.dat}
	\begin{axis}[
		xmin=0,xmax=6,
		ymin=-2,ymax=5,
	]
	\draw[-{Stealth}] (axis cs: 1.75,0.45) %
	.. controls (axis cs:1.75,0.85) and %
	(axis cs:3.9,0.5) .. (axis cs: 3.9,1.05);
	\draw[decoration={raise=2pt,text along path,%
	text={|\tiny| Better bandwidth}, %
	text align={center}}, decorate] %
	(axis cs: 1.75,0.45) %
	.. controls (axis cs:1.75,0.85) and %
	(axis cs:3.9,0.5) .. (axis cs: 3.9,1.05);
	\draw[-{Stealth}] %
	(axis cs: 1.5,0.45) .. controls %
	(axis cs:1.5,0.85) and (axis cs:1.25,1.1) %
	.. (axis cs: 1.25,1.5) .. controls %
	(axis cs:1.25,1.9) and (axis cs:1.35,1.9) %
	.. (axis cs:1.35,2.6);
	\draw[decoration={raise=2pt,text along path, %
	text={|\tiny|Very oversmoothed}, %
	text align={center}}, decorate] %
	(axis cs: 1.35,2.6) .. controls %
	(axis cs:1.35,1.9) and (axis cs:1.25,1.9) %
	.. (axis cs: 1.25,1.5) .. %
	controls (axis cs:1.25,1.1) and %
	(axis cs:1.5,0.85) .. (axis cs:1.5,0.45);
	\end{axis}
\end{tikzpicture}
\end{minted}

\subsection{Standalone Kernel Density Estimation}

Plotting only one dataset and choosing adequate plot limits, one
may obtain a simple representation of a kernel density estimation.
This can be seen in figure \ref{graph:kde_standalone}, obtained from the
code excerpt below.

\begin{figure}[h]
	\centering
	\begin{tikzpicture}
		\violinsetoptions[
			no mirror,
			reverse axis,
			data points,
		]{%
			xmin=3.3,xmax=4,
			ymin=-1,ymax=12,
			title={Plotting Some Data},
			ylabel={$p(x)$},
			xlabel={$x$},
			xmajorgrids=true,
			ymajorticks=true,
			ytick={0.0,2.5,5,7.5,10},
			yticklabels={0.0,2.5,5,7.5,10},
		}
		\violinplot[%
			index=C,
			relative position=0,
			color=red!30!black,
			label={},
			col sep=tab,
			dataset size=5pt,
			dataset color=red!10!black,
			dataset mark=*,
			dataset fill=red!90!black,
			dataset fill opacity=0.2,
		]{example.dat}
	\end{tikzpicture}
	\caption{Standalone kernel density estimation example}
	\label{graph:kde_standalone}
\end{figure}

\colorlet{mintedbgstand}{red!10!white}

\begin{minted}[%
	escapeinside=||,
	frame=single,
	rulecolor=mintedbgstand,
	framesep=10pt,
	tabsize=4,
	bgcolor=mintedbgstand]{latex}
\begin{tikzpicture}
	\violinsetoptions[
		no mirror,
		reverse axis,
		data points,
	]{%
		xmin=3.3,xmax=4,
		ymin=-1,ymax=12,
		title={Plotting Some Data},
		ylabel={$p(x)$},
		xlabel={$x$},
		xmajorgrids=true,
		ymajorticks=true,
		ytick={0.0,2.5,5,7.5,10},
		yticklabels={0.0,2.5,5,7.5,10},
	}
	\violinplot[%
		index=C,
		relative position=0,
		color=red!30!black,
		label={},
		col sep=tab,
		dataset size=5pt,
		dataset color=red!10!black,
		dataset mark=*,
		dataset fill=red!90!black,
		dataset fill opacity=0.2,
	]{example.dat}
\end{tikzpicture}
\end{minted}

\subsection{Asymmetrical Violin Plots --- Real World Data}

Using the key \texttt{invert=true} in the options of
\texttt{\textbackslash violinplot} one can plot two different sets of data side
by side, in an asymmetrical violin plot. This can be seen in figure
\ref{graph:asymmetrical}, that exhibits the male and female life expectancy at
birth, in 2019, for several countries, segregated in the WHO regions.\footnote{
	\url{https://www.who.int/data/gho/publications/world-health-statistics}.
}.

\colorlet{malecolor}{cyan!60!white}
\colorlet{femalecolor}{cyan!60!black}

\begin{figure}[h]
	\centering
	\begin{tikzpicture}
		\violinsetoptions[
			no mirror,
			scaled,
		]{%
			xmin=0.8,xmax=13.2,
			ymin=30,ymax=100,
			title={Life Expectancy Per WHO Region},
			xlabel={WHO Region},
			ylabel={Life Expectancy in Years},
			xlabel style={
				yshift = {-3*width("a")}
			},
			ymajorgrids=true,
		}
		\violinplot[%
			index=LifeExpectancyAtBirthMale,
			relative position=2,
			color=malecolor,
			label={Africa},
			col sep=comma,
			invert=true
		]{AFR.csv}
		\violinplot[%
			index=LifeExpectancyAtBirthFemale,
			relative position=2,
			color=femalecolor,
			label={},
			col sep=comma,
		]{AFR.csv}
		\violinplot[%
			index=LifeExpectancyAtBirthMale,
			relative position=4,
			color=malecolor,
			label={Middle-East},
			col sep=comma,
			invert=true
		]{EMR.csv}
		\violinplot[%
			index=LifeExpectancyAtBirthFemale,
			relative position=4,
			color=femalecolor,
			label={},
			col sep=comma,
		]{EMR.csv}
		\violinplot[%
			index=LifeExpectancyAtBirthMale,
			relative position=6,
			color=malecolor,
			label={Pacific},
			col sep=comma,
			invert=true
		]{WPR.csv}
		\violinplot[%
			index=LifeExpectancyAtBirthFemale,
			relative position=6,
			color=femalecolor,
			label={},
			col sep=comma,
		]{WPR.csv};
		\violinplot[%
			index=LifeExpectancyAtBirthMale,
			relative position=8,
			color=malecolor,
			label={SE. Asia},
			col sep=comma,
			invert=true
		]{SEAR.csv}
		\violinplot[%
			index=LifeExpectancyAtBirthFemale,
			relative position=8,
			color=femalecolor,
			label={},
			col sep=comma,
		]{SEAR.csv}
		\violinplot[%
			index=LifeExpectancyAtBirthMale,
			relative position=10,
			color=malecolor,
			label={Americas},
			col sep=comma,
			invert=true
		]{AMR.csv}
		\violinplot[%
			index=LifeExpectancyAtBirthFemale,
			relative position=10,
			color=femalecolor,
			label={},
			col sep=comma,
		]{AMR.csv}
		\violinplot[%
			index=LifeExpectancyAtBirthMale,
			relative position=12,
			color=malecolor,
			label={Europe},
			col sep=comma,
			invert=true
		]{EUR.csv}
		\violinplot[%
			index=LifeExpectancyAtBirthFemale,
			relative position=12,
			color=femalecolor,
			label={},
			col sep=comma,
		]{EUR.csv}
		\begin{axis}[
			xmin=0.8,xmax=13.2,
			ymin=30,ymax=100,
		]
			\draw(axis cs:2,35) node[anchor=east] {\male};
			\draw(axis cs:2,35) node[anchor=west] {\female};
			\draw(axis cs:4,35) node[anchor=east] {\male};
			\draw(axis cs:4,35) node[anchor=west] {\female};
			\draw(axis cs:6,35) node[anchor=east] {\male};
			\draw(axis cs:6,35) node[anchor=west] {\female};
			\draw(axis cs:8,35) node[anchor=east] {\male};
			\draw(axis cs:8,35) node[anchor=west] {\female};
			\draw(axis cs:10,35) node[anchor=east] {\male};
			\draw(axis cs:10,35) node[anchor=west] {\female};
			\draw(axis cs:12,35) node[anchor=east] {\male};
			\draw(axis cs:12,35) node[anchor=west] {\female};
		\end{axis}
	\end{tikzpicture}
	\caption{Example of asymmetrical plot}
	\label{graph:asymmetrical}
\end{figure}

The code for figure \ref{graph:asymmetrical} is avaliable below:

\begin{minted}[%
	escapeinside=||,
	frame=single,
	rulecolor=cyan!10!white,
	framesep=10pt,
	tabsize=4,
	bgcolor=cyan!10!white]{latex}
\begin{tikzpicture}
	\violinsetoptions[
		no mirror,
		scaled,
	]{%
		xmin=0,xmax=14,
		ymin=30,ymax=100,
		title={Life Expectancy Per WHO Region},
		xlabel={WHO Region},
		ylabel={Life Expectancy in Years},
		xlabel style={
			yshift = {-3*width("a")}
		},
		ymajorgrids=true,
	}
	\violinplot[%
		index=LifeExpectancyAtBirthMale,
		relative position=2,
		color=malecolor,
		label={AFR},
		col sep=comma,
		invert=true
	]{AFR.csv}
	\violinplot[%
		index=LifeExpectancyAtBirthFemale,
		relative position=2,
		color=femalecolor,
		label={},
		col sep=comma,
	]{AFR.csv}
	\violinplot[%
		index=LifeExpectancyAtBirthMale,
		relative position=4,
		color=malecolor,
		label={EMR},
		col sep=comma,
		invert=true
	]{EMR.csv}
	\violinplot[%
		index=LifeExpectancyAtBirthFemale,
		relative position=4,
		color=femalecolor,
		label={},
		col sep=comma,
	]{EMR.csv}
	\violinplot[%
		index=LifeExpectancyAtBirthMale,
		relative position=6,
		color=malecolor,
		label={WPR},
		col sep=comma,
		invert=true
	]{WPR.csv}
	\violinplot[%
		index=LifeExpectancyAtBirthFemale,
		relative position=6,
		color=femalecolor,
		label={},
		col sep=comma,
	]{WPR.csv};
	\violinplot[%
		index=LifeExpectancyAtBirthMale,
		relative position=8,
		color=malecolor,
		label={SEAR},
		col sep=comma,
		invert=true
	]{SEAR.csv}
	\violinplot[%
		index=LifeExpectancyAtBirthFemale,
		relative position=8,
		color=femalecolor,
		label={},
		col sep=comma,
	]{SEAR.csv}
	\violinplot[%
		index=LifeExpectancyAtBirthMale,
		relative position=10,
		color=malecolor,
		label={AMR},
		col sep=comma,
		invert=true
	]{AMR.csv}
	\violinplot[%
		index=LifeExpectancyAtBirthFemale,
		relative position=10,
		color=femalecolor,
		label={},
		col sep=comma,
	]{AMR.csv}
	\violinplot[%
		index=LifeExpectancyAtBirthMale,
		relative position=12,
		color=malecolor,
		label={EUR},
		col sep=comma,
		invert=true
	]{EUR.csv}
	\violinplot[%
		index=LifeExpectancyAtBirthFemale,
		relative position=12,
		color=femalecolor,
		label={},
		col sep=comma,
	]{EUR.csv}
	\begin{axis}[
		xmin=0,xmax=14,
		ymin=30,ymax=100,
	]
		\draw(axis cs:2,35) node[anchor=east] {\male};
		\draw(axis cs:2,35) node[anchor=west] {\female};
		\draw(axis cs:4,35) node[anchor=east] {\male};
		\draw(axis cs:4,35) node[anchor=west] {\female};
		\draw(axis cs:6,35) node[anchor=east] {\male};
		\draw(axis cs:6,35) node[anchor=west] {\female};
		\draw(axis cs:8,35) node[anchor=east] {\male};
		\draw(axis cs:8,35) node[anchor=west] {\female};
		\draw(axis cs:10,35) node[anchor=east] {\male};
		\draw(axis cs:10,35) node[anchor=west] {\female};
		\draw(axis cs:12,35) node[anchor=east] {\male};
		\draw(axis cs:12,35) node[anchor=west] {\female};
	\end{axis}
\end{tikzpicture}
\end{minted}

\section{Limitations}

As the math is handled through \TeX, generating the kernel distribution
estimations is a slow process; therefore, if possible, the number of samples
should not be very large. Also, each value should not, itself, be too large,
to avoid ``dimension too large'' errors. In this case, the data should be scaled
and the tick labels manually corrected.

As each violin plot is rendered in a different \texttt{axis} environment,
the positions of the axes' labels are not, usually, correct. One should
set it manually. In the examples, this was accomplished setting the
vertical/horizontal shift to twice the height of a letter, in the direction
opposite to the appropriate axis. This approach also leads to another problem:
using the keys ``height'' or ``width'' won't rescale the plot; one might use,
before and after the code for the figure, \texttt{\textbackslash pgfplotsset},
as shown below:

\colorlet{mintedbgtempplotsset}{red!50!blue}
\colorlet{mintedbgplotsset}{mintedbgtempplotsset!10!white}

\begin{minted}[%
	escapeinside=||,
	frame=single,
	rulecolor=mintedbgplotsset,
	framesep=10pt,
	tabsize=4,
	bgcolor=mintedbgplotsset]{latex}
\pgfplotsset{height=1.75\linewidth}
\begin{tikzpicture}
	\violinsetoptions[
		|\textit{options for \texttt{tikzviolinplots}}|
	]{
		|\textit{\texttt{pgfplots} options}|
	}
	\violinplot[%
		|\textit{options for data points}|
	]{|\textit{filename}|}
	|...|
\end{tikzpicture}
\pgfplotsset{height=0.9\linewidth}
\end{minted}

For the same reason, the options \texttt{xmin}, \texttt{ymin}, \texttt{xmax}
and \texttt{ymax} are required; no automatic placement is performed.

\end{document}