-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarGraphStack.py
More file actions
160 lines (121 loc) · 5.19 KB
/
barGraphStack.py
File metadata and controls
160 lines (121 loc) · 5.19 KB
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
#multiple day plot
import matplotlib
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from csvTool import csvColorTool
class DataCollect:
def __init__(self, ct):
self.header = [ct.red.cat, ct.blue.cat, ct.white.cat, ct.green.cat, ct.yellow.cat]
self.filename = ct.filename
self.ct = ct
def readCSV(self):
self.data_r = pd.read_csv(self.filename, delimiter = ',')
# Takes total data and creates sub arrays for each bar category
def gatherData(self, rawData): # make better plz
dataSize = len(rawData)
dataR = []
dataB = []
dataW = []
dataG = []
dataY = []
for i in range(0, 5):
print rawData.iloc[0].iloc[i]
# [rawData.iloc[color].iloc[field]] where field == 1 == 'time'
dataR += [rawData.iloc[0].iloc[1]]
dataB += [rawData.iloc[1].iloc[1]]
dataW += [rawData.iloc[2].iloc[1]]
dataG += [rawData.iloc[3].iloc[1]]
dataY += [rawData.iloc[4].iloc[1]]
'''
for day in range(0, dataSize):
dataR += [rawData.iloc[day].iloc[0]]
dataB += [rawData.iloc[day].iloc[1]]
dataW += [rawData.iloc[day].iloc[2]]
dataG += [rawData.iloc[day].iloc[3]]
dataY += [rawData.iloc[day].iloc[4]]
'''
# Necessary for creating a stacked bar graph
# This is for the "bottom" parameter of the plot
dr = np.array(dataR) # dr --> data Red
db = np.array(dataB) # db --> data Blue
dw = np.array(dataW) # dw --> data White
dg = np.array(dataG) # dg --> data Green
dy = np.array(dataY) # dy --> data Yellow
return [dr, db, dw, dg, dy]
class BarGraph:
def __init__(self):
self.ct = csvColorTool()
#self.filename = self.ct.filename
self.dc = DataCollect(self.ct)
self.dataGrabSize_ = 10
self.x = self.defineBarCount()
def setDataSize(self, num):
dataGrabSize_ = num
def defineBarCount(self):
x = []
for i in range(0, self.dataGrabSize_):
x += [i]
return x
# Annotate every bar with data
def labeler(self, rect):
for rect in rects:
h = rect.get_height()
ax.annotate('{}'.format(h),
xy = (rect.get_x() + rect.get_width() / 2, h),
xytext = (0, 0),
textcoords = "offset points",
ha = 'center',
va = 'bottom')
# Label every bar
def labelBars(self, barList):
for bar in barList:
self.labeler(bar)
# Return x-axis list of days --> [D1, D2, ... , Dn]
def getDayLabels(self):
days = []
for i in range(0, self.dataGrabSize_):
dayNumber = str(i + 1)
days += ['D' + dayNumber]
return days
def setVisualCharacteristics(self, barList, ax):
cats = self.dc.header
# x-axis
days = self.getDayLabels()
ax.set_xticklabels(days)
ax.set_xticks(self.x)
# y-axis
ax.set_ylabel('Time')
# Details
ax.legend(((barList[0][0]), barList[1][0], barList[2][0], barList[3][0], barList[4][0]), (cats[0], cats[1], cats[2], cats[3], cats[4]))
ax.set_title('Colors and Time graph')
#labelBars(barList)
return ax
def initStackedPlot(self):
# Set graph numerical characteristics
x = self.x
width = 0.15
fig, ax = plt.subplots()
ax.grid(zorder=0)
# Gather raw data into numpy data lists --> Provides data points and 'bottom' parameter for stacked bar graph
dataLists = self.dc.gatherData(self.dc.data_r)
# Collect data into each bar
redBar = ax.bar(x, dataLists[0], width, color ='r', edgecolor = 'black', zorder=3)
blueBar = ax.bar(x, dataLists[1], width, bottom = dataLists[0], color = 'b', edgecolor = 'black',zorder=3)
whiteBar = ax.bar(x, dataLists[2], width, bottom = dataLists[0] + dataLists[1], color = '#d3d3d3', edgecolor = 'black', zorder=3)
greenBar = ax.bar(x, dataLists[3], width, bottom = dataLists[0] + dataLists[1] + dataLists[2], color = 'g', edgecolor = 'black', zorder=3)
yellowBar = ax.bar(x, dataLists[4], width, bottom = dataLists[0] + dataLists[1] + dataLists[2] + dataLists [3] ,color = 'y', edgecolor = 'black', zorder=3)
barList = [redBar, blueBar, whiteBar, greenBar, yellowBar]
ax = self.setVisualCharacteristics(barList, ax)
fig.tight_layout()
return plt
def drawPlot(self):
# Assign <self.dc> a variable for visual simplicity
dataCollect = self.dc
# Read the CSV provided
dataCollect.readCSV()
# initialize the plot, and assign it to variable <plt>
plt = self.initStackedPlot()
# Illustrate the plot to user
plt.show()
#end barGraphStack