1
- import sys
2
1
import os
3
- from numpy .ma .core import size
4
- import pygame
5
2
import numpy as np
6
3
7
4
import matplotlib
8
- #matplotlib.use('pygame')
9
- matplotlib .use ('pygame' )
10
5
11
6
import matplotlib .pyplot as plt
12
- import matplotlib .figure as fg
13
-
14
7
import matplotlib .image as mpimg
15
8
16
9
10
+ matplotlib .use ("pygame" )
11
+
12
+
17
13
def plot_error_bars_ex (ax ):
18
14
# example data
19
15
x = np .array ([0.5 , 1.0 , 1.5 , 2.0 , 2.5 , 3.0 , 3.5 , 4.0 , 4.5 , 5.0 ])
@@ -24,24 +20,29 @@ def plot_error_bars_ex(ax):
24
20
# lower & upper limits of the error
25
21
lolims = np .array ([0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 ], dtype = bool )
26
22
uplims = np .array ([0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 ], dtype = bool )
27
- ls = ' dotted'
23
+ ls = " dotted"
28
24
29
25
# standard error bars
30
26
ax .errorbar (x , y , xerr = xerr , yerr = yerr , linestyle = ls )
31
27
32
28
# including upper limits
33
- ax .errorbar (x , y + 0.5 , xerr = xerr , yerr = yerr , uplims = uplims ,
34
- linestyle = ls )
29
+ ax .errorbar (x , y + 0.5 , xerr = xerr , yerr = yerr , uplims = uplims , linestyle = ls )
35
30
36
31
# including lower limits
37
- ax .errorbar (x , y + 1.0 , xerr = xerr , yerr = yerr , lolims = lolims ,
38
- linestyle = ls )
32
+ ax .errorbar (x , y + 1.0 , xerr = xerr , yerr = yerr , lolims = lolims , linestyle = ls )
39
33
40
34
# including upper and lower limits
41
- ax .errorbar (x , y + 1.5 , xerr = xerr , yerr = yerr ,
42
- lolims = lolims , uplims = uplims ,
43
- marker = 'o' , markersize = 8 ,
44
- linestyle = ls )
35
+ ax .errorbar (
36
+ x ,
37
+ y + 1.5 ,
38
+ xerr = xerr ,
39
+ yerr = yerr ,
40
+ lolims = lolims ,
41
+ uplims = uplims ,
42
+ marker = "o" ,
43
+ markersize = 8 ,
44
+ linestyle = ls ,
45
+ )
45
46
46
47
# Plot a series with lower and upper limits in both x & y
47
48
# constant x-error with varying y-error
@@ -58,15 +59,23 @@ def plot_error_bars_ex(ax):
58
59
uplims [[3 ]] = True # only limited at this index
59
60
60
61
# do the plotting
61
- ax .errorbar (x , y + 2.1 , xerr = xerr , yerr = yerr ,
62
- xlolims = xlolims , xuplims = xuplims ,
63
- uplims = uplims , lolims = lolims ,
64
- marker = 'o' , markersize = 8 ,
65
- linestyle = 'none' )
62
+ ax .errorbar (
63
+ x ,
64
+ y + 2.1 ,
65
+ xerr = xerr ,
66
+ yerr = yerr ,
67
+ xlolims = xlolims ,
68
+ xuplims = xuplims ,
69
+ uplims = uplims ,
70
+ lolims = lolims ,
71
+ marker = "o" ,
72
+ markersize = 8 ,
73
+ linestyle = "none" ,
74
+ )
66
75
67
76
# tidy up the figure
68
77
ax .set_xlim ((0 , 5.5 ))
69
- ax .set_title (' Errorbar upper and lower limits' )
78
+ ax .set_title (" Errorbar upper and lower limits" )
70
79
71
80
72
81
def plot_violin (ax ):
@@ -78,60 +87,58 @@ def adjacent_values(vals, q1, q3):
78
87
lower_adjacent_value = np .clip (lower_adjacent_value , vals [0 ], q1 )
79
88
return lower_adjacent_value , upper_adjacent_value
80
89
81
-
82
90
def set_axis_style (ax , labels ):
83
- ax .xaxis .set_tick_params (direction = ' out' )
84
- ax .xaxis .set_ticks_position (' bottom' )
91
+ ax .xaxis .set_tick_params (direction = " out" )
92
+ ax .xaxis .set_ticks_position (" bottom" )
85
93
ax .set_xticks (np .arange (1 , len (labels ) + 1 ))
86
94
ax .set_xticklabels (labels )
87
95
ax .set_xlim (0.25 , len (labels ) + 0.75 )
88
- ax .set_xlabel ('Sample name' )
89
-
96
+ ax .set_xlabel ("Sample name" )
90
97
91
98
# create test data
92
99
np .random .seed (19680801 )
93
100
data = [sorted (np .random .normal (0 , std , 100 )) for std in range (1 , 5 )]
94
101
102
+ ax .set_title ("Customized violin plot" )
103
+ parts = ax .violinplot (data , showmeans = False , showmedians = False , showextrema = False )
95
104
96
-
97
- ax .set_title ('Customized violin plot' )
98
- parts = ax .violinplot (
99
- data , showmeans = False , showmedians = False ,
100
- showextrema = False )
101
-
102
- for pc in parts ['bodies' ]:
103
- pc .set_facecolor ('#D43F3A' )
104
- pc .set_edgecolor ('black' )
105
+ for pc in parts ["bodies" ]:
106
+ pc .set_facecolor ("#D43F3A" )
107
+ pc .set_edgecolor ("black" )
105
108
pc .set_alpha (1 )
106
109
107
110
quartile1 , medians , quartile3 = np .percentile (data , [25 , 50 , 75 ], axis = 1 )
108
- whiskers = np .array ([
109
- adjacent_values (sorted_array , q1 , q3 )
110
- for sorted_array , q1 , q3 in zip (data , quartile1 , quartile3 )])
111
+ whiskers = np .array (
112
+ [
113
+ adjacent_values (sorted_array , q1 , q3 )
114
+ for sorted_array , q1 , q3 in zip (data , quartile1 , quartile3 )
115
+ ]
116
+ )
111
117
whiskers_min , whiskers_max = whiskers [:, 0 ], whiskers [:, 1 ]
112
118
113
119
inds = np .arange (1 , len (medians ) + 1 )
114
- ax .scatter (inds , medians , marker = 'o' , color = ' white' , s = 30 , zorder = 3 )
115
- ax .vlines (inds , quartile1 , quartile3 , color = 'k' , linestyle = '-' , lw = 5 )
116
- ax .vlines (inds , whiskers_min , whiskers_max , color = 'k' , linestyle = '-' , lw = 1 )
120
+ ax .scatter (inds , medians , marker = "o" , color = " white" , s = 30 , zorder = 3 )
121
+ ax .vlines (inds , quartile1 , quartile3 , color = "k" , linestyle = "-" , lw = 5 )
122
+ ax .vlines (inds , whiskers_min , whiskers_max , color = "k" , linestyle = "-" , lw = 1 )
117
123
118
124
# set style for the axes
119
- labels = ['A' , 'B' , 'C' , 'D' ]
125
+ labels = ["A" , "B" , "C" , "D" ]
120
126
set_axis_style (ax , labels )
121
127
122
- fig , axes = plt .subplots (3 ,2 ,figsize = (16 , 12 ))
128
+
129
+ fig , axes = plt .subplots (3 , 2 , figsize = (16 , 12 ))
123
130
print (type (fig ))
124
131
125
- axes [0 , 0 ].plot ([1 ,2 ], [1 ,2 ], color = ' green' , label = ' test' )
126
- axes [0 , 0 ].plot ([1 ,2 ], [1 ,1 ], color = ' orange' , lw = 5 , label = ' test other larger' )
132
+ axes [0 , 0 ].plot ([1 , 2 ], [1 , 2 ], color = " green" , label = " test" )
133
+ axes [0 , 0 ].plot ([1 , 2 ], [1 , 1 ], color = " orange" , lw = 5 , label = " test other larger" )
127
134
# axes[0, 0].legend()
128
- axes [0 , 1 ].text (0.5 , 0.5 , '2' , size = 50 )
129
- axes [1 , 0 ].set_xlabel (' swag' )
135
+ axes [0 , 1 ].text (0.5 , 0.5 , "2" , size = 50 )
136
+ axes [1 , 0 ].set_xlabel (" swag" )
130
137
axes [1 , 0 ].fill_between ([0 , 1 , 2 ], [1 , 2 , 3 ], [3 , 4 , 5 ])
131
138
axes [1 , 0 ].scatter ([0 , 1 , 2 ], [2 , 3 , 4 ], s = 50 )
132
- axes [1 , 1 ].imshow (mpimg .imread (' images' + os .sep + ' long_dog.jpg' ))
139
+ axes [1 , 1 ].imshow (mpimg .imread (" images" + os .sep + " long_dog.jpg" ))
133
140
plot_error_bars_ex (axes [2 , 1 ])
134
141
135
142
plot_violin (axes [2 , 0 ])
136
143
137
- plt .show ()
144
+ plt .show ()
0 commit comments