-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentigraph.py
183 lines (154 loc) · 5.5 KB
/
sentigraph.py
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import numpy as np
import string
import sqlite3
import os
import re
from os.path import isfile,abspath,isdir,join
from collections import defaultdict
import matplotlib
matplotlib.use("Pdf")
from matplotlib import pyplot as plt
from matplotlib import cm as cm
my_dpi = 80
left = 0.05
right = 0.95
x = 1000
y = 800
def plotting(folder,title,indexes,cols,scores,header):
plt.figure(num=title, figsize=(x/my_dpi, y/my_dpi), dpi=my_dpi, facecolor='w', edgecolor='k')
plt.xlabel('Entry')
plt.ylabel('Values')
plt.suptitle(header)
plt.subplots_adjust(top=0.8)
for i in range(len(cols)):
plt.plot(indexes,scores[i], label=cols[i])
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
ncol=2, mode="expand", borderaxespad=0.)
plt.savefig(folder+title+"_joined.png")
plt.clf()
plt.close()
def plotting_separated(folder,title,dicts,df,header):
"""plots on separate graph in the same window"""
plt.figure(num='Separate graphs of lexicons',figsize=(x/my_dpi, y/my_dpi), dpi=my_dpi)
plt.suptitle(header)
plt.subplots_adjust(left=left,right=right)
indexes = [x for x in range(len(df.index))]
for i in range(len(dicts)):
values = df[dicts[i]].tolist()
plt.yticks([0.25*x for x in range(-4,5,1)],[str(0.25*x) for x in range(-4,5,1)])
ax = plt.subplot(2,4,i+1)
ax.plot(indexes, values, linewidth=2, linestyle='solid')
plt.ylim(-1,1)
plt.title(dicts[i])
plt.savefig(folder+title+"_separated.png")
plt.clf()
plt.close()
def faceting(folder,sentence,df,index):
# ------- PART 2: Apply to all individuals
# initialize the figure
plt.figure(num='Sentence spider analysis',figsize=(x/my_dpi, y/my_dpi), dpi=my_dpi)
plt.suptitle(sentence)
plt.subplots_adjust(left=left,right=right)
# Create a color palette:
my_palette = plt.cm.get_cmap("Set2", len(df.index))
# Loop to plot
for row in range(len(df.index)):
make_spider(df, row=row, title=df['group'][row], color=my_palette(row))
plt.savefig(folder+str(index)+"spider.png")
plt.clf()
plt.close()
def make_spider(df, row, title, color):
# number of variable
categories=list(df)[1:]
N = len(categories)
pi = 3.141
# What will be the angle of each axis in the plot? (we divide the plot / number of variable)
angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:1]
# Initialise the spider plot
ax = plt.subplot(2,4,row+1, polar=True, )
# If you want the first axis to be on top:
ax.set_theta_offset(pi / 2)
ax.set_theta_direction(-1)
# Draw one axe per variable + add labels labels yet
plt.xticks(angles[:-1], categories, color='grey', size=8)
# Draw ylabels
ax.set_rlabel_position(0)
plt.yticks([0.25,0.5,0.75], ["0,25","0,5","0,75"], color="grey", size=7)
plt.ylim(0,1)
# Ind1
values=df.loc[row].drop('group').values.flatten().tolist()
values += values[:1]
ax.plot(angles, values, color=color, linewidth=2, linestyle='solid')
ax.fill(angles, values, color=color, alpha=0.4)
# Add a title
plt.title(title, size=11, color=color, y=1.1)
def bar_compare(folder,x_axis,title,list1,list2):
# data to plot
n_groups = len(x_axis)
# create plot
_, _ = plt.subplots()
fig = plt.figure()
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.8
_ = plt.bar(index, list1, bar_width,
alpha=opacity,
color='g',
label='Correct')
_ = plt.bar(index + bar_width, list2, bar_width,
alpha=opacity,
color='b',
label='Solved')
fig.autofmt_xdate()
plt.xlabel('Dictionary')
plt.ylabel('Count')
plt.title(title)
plt.xticks(index + bar_width, x_axis)
plt.legend()
plt.savefig(folder+"barplot.png")
plt.clf()
plt.close()
def draw_pies(folder,names,title,labels,verdicts,dimx,dimy):
"""
plots in order of labels pie charts for every dictionary in the same window.
"""
plt.figure(num='Pie charts',figsize=(x/my_dpi, y/my_dpi), dpi=my_dpi)
plt.suptitle(title)
plt.subplots_adjust(left=left,right=right)
for i in range(len(verdicts)):
plt.subplot(dimx,dimy,i+1)
plt.pie(verdicts[i],labels=labels, startangle=90)
plt.title(names[i])
plt.savefig(folder+'piecharts.png')
plt.clf()
plt.close()
def corr_matrix(folder,data_array,labels,category):
"""plot the pearson correlation matrix with given labels"""
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xticks(np.arange(len(labels)))
ax1.set_yticks(np.arange(len(labels)))
ax1.set_xticklabels(labels,fontsize=6)
ax1.set_yticklabels(labels,fontsize=6)
ax1.grid(True)
cmap = cm.get_cmap('jet', 30)
cax = ax1.imshow(data_array, interpolation="nearest", cmap=cmap)
plt.title('Correlation matrix: '+category)
# Add colorbar, make sure to specify tick locations to match desired ticklabels
fig.colorbar(cax, ticks=[.1,.2,.3,.4,.5,.6,.7,.8,.9,1])
fig.autofmt_xdate()
plt.savefig(folder+"corr_"+category+".png")
plt.clf()
plt.close()
def bar_values(folder,labels,values,key,title):
"""draws a bar chart with given labels and values"""
fig = plt.figure()
plt.bar(labels, values)
plt.xlabel('Categories')
plt.ylabel('Values')
fig.autofmt_xdate()
plt.title("Bar chart "+title)
plt.savefig(folder+"bar_"+key+".png")
plt.clf()
plt.close()