1+ import numpy as np
2+ import matplotlib .pyplot as plt
3+
4+ def plot_error_bars_ex (ax ):
5+ # example data
6+ x = np .array ([0.5 , 1.0 , 1.5 , 2.0 , 2.5 , 3.0 , 3.5 , 4.0 , 4.5 , 5.0 ])
7+ y = np .exp (- x )
8+ xerr = 0.1
9+ yerr = 0.2
10+
11+ # lower & upper limits of the error
12+ lolims = np .array ([0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 ], dtype = bool )
13+ uplims = np .array ([0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 ], dtype = bool )
14+ ls = 'dotted'
15+
16+ # standard error bars
17+ ax .errorbar (x , y , xerr = xerr , yerr = yerr , linestyle = ls )
18+
19+ # including upper limits
20+ ax .errorbar (x , y + 0.5 , xerr = xerr , yerr = yerr , uplims = uplims ,
21+ linestyle = ls )
22+
23+ # including lower limits
24+ ax .errorbar (x , y + 1.0 , xerr = xerr , yerr = yerr , lolims = lolims ,
25+ linestyle = ls )
26+
27+ # including upper and lower limits
28+ ax .errorbar (x , y + 1.5 , xerr = xerr , yerr = yerr ,
29+ lolims = lolims , uplims = uplims ,
30+ marker = 'o' , markersize = 8 ,
31+ linestyle = ls )
32+
33+ # Plot a series with lower and upper limits in both x & y
34+ # constant x-error with varying y-error
35+ xerr = 0.2
36+ yerr = np .full_like (x , 0.2 )
37+ yerr [[3 , 6 ]] = 0.3
38+
39+ # mock up some limits by modifying previous data
40+ xlolims = lolims
41+ xuplims = uplims
42+ lolims = np .zeros_like (x )
43+ uplims = np .zeros_like (x )
44+ lolims [[6 ]] = True # only limited at this index
45+ uplims [[3 ]] = True # only limited at this index
46+
47+ # do the plotting
48+ ax .errorbar (x , y + 2.1 , xerr = xerr , yerr = yerr ,
49+ xlolims = xlolims , xuplims = xuplims ,
50+ uplims = uplims , lolims = lolims ,
51+ marker = 'o' , markersize = 8 ,
52+ linestyle = 'none' )
53+
54+ # tidy up the figure
55+ ax .set_xlim ((0 , 5.5 ))
56+ ax .set_title ('Errorbar upper and lower limits' )
57+
58+
59+ def plot_violin (ax ):
60+ def adjacent_values (vals , q1 , q3 ):
61+ upper_adjacent_value = q3 + (q3 - q1 ) * 1.5
62+ upper_adjacent_value = np .clip (upper_adjacent_value , q3 , vals [- 1 ])
63+
64+ lower_adjacent_value = q1 - (q3 - q1 ) * 1.5
65+ lower_adjacent_value = np .clip (lower_adjacent_value , vals [0 ], q1 )
66+ return lower_adjacent_value , upper_adjacent_value
67+
68+
69+ def set_axis_style (ax , labels ):
70+ ax .xaxis .set_tick_params (direction = 'out' )
71+ ax .xaxis .set_ticks_position ('bottom' )
72+ ax .set_xticks (np .arange (1 , len (labels ) + 1 ))
73+ ax .set_xticklabels (labels )
74+ ax .set_xlim (0.25 , len (labels ) + 0.75 )
75+ ax .set_xlabel ('Sample name' )
76+
77+
78+ # create test data
79+ np .random .seed (19680801 )
80+ data = [sorted (np .random .normal (0 , std , 100 )) for std in range (1 , 5 )]
81+
82+
83+
84+ ax .set_title ('Customized violin plot' )
85+ parts = ax .violinplot (
86+ data , showmeans = False , showmedians = False ,
87+ showextrema = False )
88+
89+ for pc in parts ['bodies' ]:
90+ pc .set_facecolor ('#D43F3A' )
91+ pc .set_edgecolor ('black' )
92+ pc .set_alpha (1 )
93+
94+ quartile1 , medians , quartile3 = np .percentile (data , [25 , 50 , 75 ], axis = 1 )
95+ whiskers = np .array ([
96+ adjacent_values (sorted_array , q1 , q3 )
97+ for sorted_array , q1 , q3 in zip (data , quartile1 , quartile3 )])
98+ whiskers_min , whiskers_max = whiskers [:, 0 ], whiskers [:, 1 ]
99+
100+ inds = np .arange (1 , len (medians ) + 1 )
101+ ax .scatter (inds , medians , marker = 'o' , color = 'white' , s = 30 , zorder = 3 )
102+ ax .vlines (inds , quartile1 , quartile3 , color = 'k' , linestyle = '-' , lw = 5 )
103+ ax .vlines (inds , whiskers_min , whiskers_max , color = 'k' , linestyle = '-' , lw = 1 )
104+
105+ # set style for the axes
106+ labels = ['A' , 'B' , 'C' , 'D' ]
107+ set_axis_style (ax , labels )
0 commit comments