-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataPlot.py
More file actions
120 lines (98 loc) · 3.17 KB
/
dataPlot.py
File metadata and controls
120 lines (98 loc) · 3.17 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
from bokeh.plotting import show,output_notebook,ColumnDataSource,figure,vplot
from bokeh.models import HoverTool
from bokeh.charts import Bar
from numpy import pi
output_notebook()
class dataPrep():
def __init__(self,table,df):
self.states = table.keys()
self.stateCount = table.values()
self.stateList = sorted(df.columns.tolist())
self.df = df
def colorChooser(self,c):
# selects colors to use for heatmap
colors = [
"#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce",
"#ddb7b1", "#cc7878", "#933b41", "#550b1d", "#ffffff"
]
if c == 0.0:
return colors[0]
elif c <= 0.005:
return colors[1]
elif c <= 0.01:
return colors[2]
elif c <= 0.025:
return colors[3]
elif c <= 0.05:
return colors[4]
elif c <= 0.1:
return colors[5]
elif c <= 0.25:
return colors[6]
elif c <= 0.5:
return colors[7]
elif c <= 0.75:
return colors[8]
elif c >= 1:
return colors[9]
class dataPlots(dataPrep):
def __init__(self,table,df,mx):
dataPrep.__init__(self,table,df) #what was mx?
self.matrix = df.sort_index()
self.maxVal = mx
def stateBarPlot(self):
# plots the number of awards for each contiguous state
stateBarPlot = Bar(self.stateCount,cat=self.states,tools='',
title="Number of Awards per State",
xlabel='States',ylabel='Awards per State',
width=800,height=400)
show(stateBarPlot)
def __heatmapPrep(self):
# prepares data for use in heatmap
self.states = self.df.columns.tolist()
self.keywords = self.df.index.tolist()
# tchange to heatmap plot
state = []
kword = []
color = []
value = []
for k in self.keywords:
for s in self.states:
state.append(s)
kword.append(k)
val = self.df[s][k]
color.append(self.colorChooser(val))
value.append(int(val*self.maxVal))
source = ColumnDataSource(
data=dict(
state=state,
kword=kword,
color=color,
value=value,
)
)
return(source)
def heatmap(self,name):
# plots heatmap
source = self.__heatmapPrep()
hover = HoverTool(
tooltips = [
("state,term","@state,@kword"),
("value","@value"),
]
)
p = figure(
tools=[hover],
plot_width=900, plot_height=450,
# x_axis_location="above",
x_range=self.states, y_range=list(reversed(self.keywords)),
title=name
)
p.rect('state', 'kword', 1, 1, source=source, color='color', line_color=None)
p.grid.grid_line_color = None
p.axis.axis_line_color = None
p.axis.major_tick_line_color = None
p.axis.major_label_standoff = 0
p.legend
p.xaxis.major_label_orientation = pi/3
show(p)