comparison src/breadcrumbs/src/BoxPlot.py @ 0:2f4f6f08c8c4 draft

Uploaded
author george-weingart
date Tue, 13 May 2014 21:58:57 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:2f4f6f08c8c4
1 """
2 Author: Timothy Tickle
3 Description: Class to create box plots.
4 """
5
6 #####################################################################################
7 #Copyright (C) <2012>
8 #
9 #Permission is hereby granted, free of charge, to any person obtaining a copy of
10 #this software and associated documentation files (the "Software"), to deal in the
11 #Software without restriction, including without limitation the rights to use, copy,
12 #modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
13 #and to permit persons to whom the Software is furnished to do so, subject to
14 #the following conditions:
15 #
16 #The above copyright notice and this permission notice shall be included in all copies
17 #or substantial portions of the Software.
18 #
19 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
20 #INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
21 #PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #####################################################################################
26
27 __author__ = "Timothy Tickle"
28 __copyright__ = "Copyright 2012"
29 __credits__ = ["Timothy Tickle"]
30 __license__ = "MIT"
31 __maintainer__ = "Timothy Tickle"
32 __email__ = "ttickle@sph.harvard.edu"
33 __status__ = "Development"
34
35 #External libraries
36 from ConstantsFiguresBreadCrumbs import ConstantsFiguresBreadCrumbs
37 import matplotlib.pyplot as plt
38 from pylab import *
39
40 #Plots a matrix
41 class BoxPlot:
42
43 @staticmethod
44 def funcPlot(ly, lsLabels, strOutputFigurePath, strTitle = "Title", strXTitle="X Axis", strYTitle="Y Axis", strColor = "#83C8F9", fJitter=False, fInvert=False, fInvertY=False):
45 """
46 Plot a box plot with optional jittering.
47
48 :params ly: List of y values
49 :type: List of doubles
50 :params lsLabels: List of labels (x tick lables)
51 :type: List string
52 :params strOutputFigurePath: File path to make figure
53 :type: String file path
54 :params strTitle: Title of figure
55 :type: String
56 :params strXTitle: Label of x axis
57 :type: String
58 :params strYTitle: Label of y axis
59 :type: String
60 :params strColor: Hex color for the face of the boxplots
61 :type: String
62 :params fJitter: Indicator of jittering (true) or not (false)
63 :type: Boolean
64 :params fInvert: Invert colors (true)
65 :type: Boolean
66 :params fInvertY: Invert y axis
67 :type: Boolean
68 """
69
70 #Start plot
71 #Get plot object
72 imgFigure = plt.figure()
73
74 #Get plot colorsstrOutFigure
75 objFigureControl = ConstantsFiguresBreadCrumbs()
76 #Boxplots have to be plotted over the scatter so the alpha can not go to 1.0
77 #In this case capturing the alpha before inversion
78 #Inversion automoatically sets it to 1.
79 dAlpha=objFigureControl.c_dAlpha
80 objFigureControl.invertColors(fInvert=fInvert)
81
82 #Color/Invert figure
83 imgFigure.set_facecolor(objFigureControl.c_strBackgroundColorWord)
84 imgSubplot = imgFigure.add_subplot(111,axisbg=objFigureControl.c_strBackgroundColorLetter)
85 imgSubplot.set_xlabel(strXTitle)
86 imgSubplot.set_ylabel(strYTitle)
87 imgSubplot.spines['top'].set_color(objFigureControl.c_strDetailsColorLetter)
88 imgSubplot.spines['bottom'].set_color(objFigureControl.c_strDetailsColorLetter)
89 imgSubplot.spines['left'].set_color(objFigureControl.c_strDetailsColorLetter)
90 imgSubplot.spines['right'].set_color(objFigureControl.c_strDetailsColorLetter)
91 imgSubplot.xaxis.label.set_color(objFigureControl.c_strDetailsColorLetter)
92
93 #Adds light grid for numbers and puts them in the background
94 imgSubplot.yaxis.grid(True, linestyle='-', which='major', color=objFigureControl.c_strGridLineColor, alpha=objFigureControl.c_dAlpha)
95 imgSubplot.set_axisbelow(True)
96 imgSubplot.yaxis.label.set_color(objFigureControl.c_strDetailsColorLetter)
97 imgSubplot.tick_params(axis='x', colors=objFigureControl.c_strDetailsColorLetter)
98 imgSubplot.tick_params(axis='y', colors=objFigureControl.c_strDetailsColorLetter)
99 charMarkerEdgeColor = objFigureControl.c_strDetailsColorLetter
100
101 #Make box plot
102 bp = plt.boxplot(x=ly, notch=1, patch_artist=True)
103 for iindex, ldData in enumerate(ly):
104 ldX = None
105 if fJitter:
106 ldX = [float(iindex+1)+ uniform(-.05,.05) for x in xrange(len(ldData))]
107 else:
108 ldX = [float(iindex+1) for x in xrange(len(ldData))]
109 plt.scatter(x=ldX,y=ly[iindex],c=strColor,marker="o",alpha=objFigureControl.c_dAlpha)
110
111 #Color boxes
112 plt.setp(bp['boxes'], color=objFigureControl.c_strDetailsColorLetter, facecolor=strColor, alpha=dAlpha)
113 plt.setp(bp['whiskers'], color=objFigureControl.c_strDetailsColorLetter)
114
115 #Set ticks and title
116 lsLabelsWithCounts = []
117 for iindex,sCurLabel in enumerate(lsLabels):
118 lsLabelsWithCounts.append(sCurLabel+" ( "+str(len(ly[iindex]))+" )")
119 xtickNames = plt.setp(imgSubplot, xticklabels=lsLabelsWithCounts)
120 imgSubplot.set_title(strTitle)
121 imgSubplot.title.set_color(objFigureControl.c_strDetailsColorLetter)
122
123 #Invert Y axis
124 if fInvertY:
125 ax = plt.gca()
126 ax.set_ylim(ax.get_ylim()[::-1])
127
128 #End plot
129 #Save to a file
130 imgFigure.savefig(strOutputFigurePath, facecolor=imgFigure.get_facecolor())