1- import sys
21import os
3- from numpy .ma .core import size
4- import pygame
52import numpy as np
63
74import matplotlib
8- #matplotlib.use('pygame')
9- matplotlib .use ('pygame' )
105
116import matplotlib .pyplot as plt
12- import matplotlib .figure as fg
13-
147import matplotlib .image as mpimg
158
169
10+ matplotlib .use ("pygame" )
11+
12+
1713def plot_error_bars_ex (ax ):
1814 # example data
1915 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):
2420 # lower & upper limits of the error
2521 lolims = np .array ([0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 ], dtype = bool )
2622 uplims = np .array ([0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 ], dtype = bool )
27- ls = ' dotted'
23+ ls = " dotted"
2824
2925 # standard error bars
3026 ax .errorbar (x , y , xerr = xerr , yerr = yerr , linestyle = ls )
3127
3228 # 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 )
3530
3631 # 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 )
3933
4034 # 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+ )
4546
4647 # Plot a series with lower and upper limits in both x & y
4748 # constant x-error with varying y-error
@@ -58,15 +59,23 @@ def plot_error_bars_ex(ax):
5859 uplims [[3 ]] = True # only limited at this index
5960
6061 # 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+ )
6675
6776 # tidy up the figure
6877 ax .set_xlim ((0 , 5.5 ))
69- ax .set_title (' Errorbar upper and lower limits' )
78+ ax .set_title (" Errorbar upper and lower limits" )
7079
7180
7281def plot_violin (ax ):
@@ -78,60 +87,58 @@ def adjacent_values(vals, q1, q3):
7887 lower_adjacent_value = np .clip (lower_adjacent_value , vals [0 ], q1 )
7988 return lower_adjacent_value , upper_adjacent_value
8089
81-
8290 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" )
8593 ax .set_xticks (np .arange (1 , len (labels ) + 1 ))
8694 ax .set_xticklabels (labels )
8795 ax .set_xlim (0.25 , len (labels ) + 0.75 )
88- ax .set_xlabel ('Sample name' )
89-
96+ ax .set_xlabel ("Sample name" )
9097
9198 # create test data
9299 np .random .seed (19680801 )
93100 data = [sorted (np .random .normal (0 , std , 100 )) for std in range (1 , 5 )]
94101
102+ ax .set_title ("Customized violin plot" )
103+ parts = ax .violinplot (data , showmeans = False , showmedians = False , showextrema = False )
95104
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" )
105108 pc .set_alpha (1 )
106109
107110 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+ )
111117 whiskers_min , whiskers_max = whiskers [:, 0 ], whiskers [:, 1 ]
112118
113119 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 )
117123
118124 # set style for the axes
119- labels = ['A' , 'B' , 'C' , 'D' ]
125+ labels = ["A" , "B" , "C" , "D" ]
120126 set_axis_style (ax , labels )
121127
122- fig , axes = plt .subplots (3 ,2 ,figsize = (16 , 12 ))
128+
129+ fig , axes = plt .subplots (3 , 2 , figsize = (16 , 12 ))
123130print (type (fig ))
124131
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" )
127134# 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" )
130137axes [1 , 0 ].fill_between ([0 , 1 , 2 ], [1 , 2 , 3 ], [3 , 4 , 5 ])
131138axes [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" ))
133140plot_error_bars_ex (axes [2 , 1 ])
134141
135142plot_violin (axes [2 , 0 ])
136143
137- plt .show ()
144+ plt .show ()
0 commit comments