Statistics
| Branch: | Revision:

peerstreamer-logs-analyzer / lib / plot_utilities.py @ a60088f6

History | View | Annotate | Download (4.78 KB)

1
from utilities import *
2
from pandas import *
3
import matplotlib.pyplot as plt
4
import colorsys 
5
import struct
6

    
7
def plotOrderedErrorBar(x,y,yerr=None,xlab=None,ylab=None,tit=None):
8
        perm = sorted(range(len(y)), key=lambda k: y[k])
9
        x = [x[i] for i in perm]
10
        y = [y[i] for i in perm]
11
        if yerr != None:
12
                yerr = [yerr[i] for i in perm]
13

    
14
        plt.plot(range(0,len(x)), y, linestyle="dashed", marker="o", color="green")
15
        plt.errorbar(range(0,len(x)),y,yerr = yerr, linestyle="None", marker="None", color="green")
16
        plt.xticks(range(0,len(x)),x,rotation=90)
17
        plt.xlim(-1,len(x))
18
        plt.grid()
19
        if xlab:
20
                plt.xlabel(xlab)
21
        if ylab:
22
                plt.ylabel(ylab)
23
        if tit:
24
                plt.title(tit)
25
        return x
26

    
27
def plotHeatMap(mat,xlab=None,ylab=None,tit=None,maxv=None,title_distance=1.1,min_color=None):
28
        if maxv:
29
                maximum = maxv
30
        else:
31
                maximum = mat.max().max()
32
        minimum = mat.min().min() 
33
        #mat = (mat-minimum)/(maximum-minimum)
34

    
35
        fig, ax = plt.subplots()
36
        my_cmap = plt.cm.get_cmap('Reds')
37
        if min_color:
38
                my_cmap.set_under(min_color)
39
                minimum += 0.00000001 # in order to set min values to white
40

    
41
        heatmap = plt.pcolor(mat, cmap=my_cmap, alpha=1, vmin=minimum, vmax=maximum)
42
        # to make a squared graph
43
        x0,x1 = ax.get_xlim()
44
        y0,y1 = ax.get_ylim()
45
        ax.set_aspect((x1-x0)/(y1-y0))
46

    
47
        plt.colorbar()
48
#        fig = plt.gcf()
49
#        fig.set_size_inches(8, 11)
50

    
51
        ax.set_frame_on(False)
52

    
53
        ax.set_yticks(np.arange(mat.shape[0]) + 0.5, minor=False)
54
        ax.set_xticks(np.arange(mat.shape[1]) + 0.5, minor=False)
55

    
56
        ax.invert_yaxis()
57
        ax.xaxis.tick_top()
58

    
59
        ax.set_xticklabels(list(mat.columns), minor=False)
60
        ax.set_yticklabels(mat.index, minor=False)
61

    
62
        plt.xticks(rotation=90)
63
        ax.grid(False)
64
        #ax = plt.gca()
65

    
66
        #legend
67
#        cbar = plt.colorbar(heatmap)
68
#        cbar.ax.get_yaxis().set_ticks([])
69
##        for j, lab in enumerate(['$0$','$1$','$2$','$>3$']):
70
##                        cbar.ax.text(.5, (2 * j + 1) / 8.0, lab, ha='center', va='center')
71
##        cbar.ax.text(.5, 0.95, maximum, ha='center', va='center')
72
#        cbar.ax.get_yaxis().labelpad = 15
73
##        cbar.ax.set_ylabel('# of contacts', rotation=270)
74
#        cbar.ax.set_ylabel('Interval: '+str(round(minimum,2))+" - "+str(round(maximum,2)))
75

    
76
        if xlab:
77
                plt.xlabel(xlab)
78
                ax.xaxis.set_label_position('top')
79
        if ylab:
80
                plt.ylabel(ylab)
81
        if tit:
82
                plt.text(0.5, title_distance, tit, horizontalalignment='center',  fontsize=20,  transform = ax.transAxes)
83
#                plt.title(tit)
84

    
85
def scientific_formatter(x, p):
86
        scale_pow = 2
87
        return "%.2f" % (x * (10 ** scale_pow))
88

    
89
def rgb_gradient_color(value,min_value,max_value):
90
        if value > max_value:
91
                value = max_value
92
        value = rescale(value,min_value,max_value,0.0,0.1)
93
        value = 0.1 - value 
94
        rgb = colorsys.hsv_to_rgb(value,1,1)
95
        RGB = tuple(int(i*255) for i in rgb)
96
        return '#'+ struct.pack('BBB',*RGB).encode('hex')
97

    
98
def make_legend(minv,maxv,levels,text="Legend: "):
99
        legend = "<"
100
        legend += "<table border='0' cellborder = '1' cellspacing='0' cellpadding='0'>\
101
                        <tr> <td colspan='1' ><b>"+text+" </b></td> \
102
                        <td colspan='1' ><b>"+str(minv)+"</b></td> "
103
        for i in range(0,levels):
104
                legend += "<td colspan='1' bgcolor='"+rgb_gradient_color(i+1,1,levels)+"' width='5'></td>"
105

    
106
        legend += "<td colspan='1' ><b>"+str(maxv)+"</b></td> </tr> </table>"
107
        legend += ">"
108
        return legend
109

    
110

    
111
def plotBarGraph(dataframe,xlab=None,ylab=None,tit=None,ylim=None):
112
        dataframe.plot(kind='bar',legend=False) #,transparency=True)
113
        if xlab:
114
                plt.xlabel(xlab)
115
        if ylab:
116
                plt.ylabel(ylab)
117
        if tit:
118
                plt.title(tit)
119

    
120
        lims=plt.axis()
121
        if ylim:
122
                plt.ylim(ylim)
123
        ax = plt.gca()
124
        ax.set_autoscale_on(False)
125
        plt.grid()
126

    
127
def plotHistogram(h,b,xlab=None,ylab=None,tit=None,log_scale=False,label=None):
128
        if label:
129
                plt.plot(b[:-1],h,label=label)
130
        else:
131
                plt.plot(b[:-1],h)
132
        if log_scale:
133
                plt.xscale('log')
134
        if xlab:
135
                plt.xlabel(xlab)
136
        if ylab:
137
                plt.ylabel(ylab)
138
        if tit:
139
                plt.title(tit)
140

    
141
def plotHistFrequencies(h,b,xlabel,title=None,log_scale=False,ylabel=None,linestyle='-'):
142
        sb = plt.subplot(1,1,1)
143

    
144
        sb.yaxis.tick_left()
145
        sb.yaxis.set_label_position("left")
146
#        sb.set_ylim([0,1])
147
#        plot1 = sb.plot(b[:-1],h,label="Frequency distribution")
148
        plot1 = sb.plot(b[:-1],h,linewidth=2,color='blue',linestyle=linestyle)
149
        if log_scale:
150
                sb.set_xscale('log')
151
        plt.legend(loc='lower right')
152
        if ylabel:
153
                plt.ylabel(ylabel)
154
        else:
155
                plt.ylabel("Number of samples")
156
        plt.xlabel(xlabel)
157
        sb.set_ylim([0,max(h)])
158

    
159
        sumSamples = []
160
        partialSum = 0.0
161
        for v in h:
162
                        partialSum += v
163
                        sumSamples.append(partialSum)
164
        sbSum=sb.twinx()
165
        sbSum.yaxis.tick_right()
166
        sbSum.yaxis.set_label_position("right")
167
        sbSum.set_ylim([0,1])
168
        plot2 = sbSum.plot(b[:-1], (np.array(sumSamples)/sum(h)),color='black',linewidth=2,linestyle=linestyle)
169
        #plot2 = sbSum.plot(b[:-1], (np.array(sumSamples)/sum(h)),color='green',label="Integral")
170
        if log_scale:
171
                sb.set_xscale('log')
172

    
173
        if title:
174
                plt.title(title)
175
        plt.ylabel("Probability")
176
        plt.legend(loc='upper right')
177
        sbSum.yaxis.grid(color='gray', linestyle='dashed')
178
        sbSum.set_axisbelow(True)