In [1]:
import pandas as pd
from pygslib import charttable as ct
import pygslib
In [2]:
# given a pandas dataframe
df = pd.DataFrame({'A': [1., 2, 3], 'B': ['a','b','c'], 'C': ['a','b','c'], 'D': ['a','b','c']})
df
Out[2]:
A
B
C
D
0
1.0
a
a
a
1
2.0
b
b
b
2
3.0
c
c
c
In [3]:
# Create a charttable object
tabl= ct.HTMLTable()
# add a formated columns using properties text, bar (size) and cell color
tabl.addcolumn(name= "A",
text = df.A.values ,
bar=[50,80,30],
colour=['red','green','blue'], # colour in word or hex
overwrite=True)
tabl.addcolumn(name= "B",
text = ct.Leyend_cat(['c','z']).apply_colour(df.B.values),
bar=None,
colour = ct.Leyend_cat(['c','z']).apply_colour(df.B.values),
overwrite=True)
tabl.addcolumn(name= "C",
text = df.C.values ,
bar=None,
colour=['#FF0000','#800000','#FFFF00'],
overwrite=True)
# display the table
tabl.display()
A
B
C
1.0
gray
a
2.0
gray
b
3.0
blue
c
In [4]:
# first we create some data
mydata= pygslib.gslib.read_gslib_file('../datasets/cluster.dat')
mydata.head(4)
Out[4]:
Xlocation
Ylocation
Primary
Secondary
Declustering Weight
0
39.5
18.5
0.06
0.22
1.619
1
5.5
1.5
0.06
0.27
1.619
2
38.5
5.5
0.08
0.40
1.416
3
20.5
1.5
0.09
0.39
1.821
In [5]:
# create a color scale
mycolourscale = ct.Leyend_num(vmin=mydata['Primary'].min(),
vmax=mydata['Primary'].max(),
cmax = "red",
cmin = "blue",
undef = "grey",
nval= 5,
log = True)
#This is the colour scale
pd.DataFrame ({'value': mycolourscale.v, 'color': mycolourscale.c})
Out[5]:
color
value
0
gray
NaN
1
blue
0.06
2
cyan
0.335018
3
lime
1.87061
4
yellow
10.4448
5
red
58.32
In [6]:
# Now we get a color scale applied to the data column 'Primary'
t_color = mycolourscale.apply_colour(mydata['Primary'].values)
s_color = mycolourscale.apply_colour(mydata['Secondary'].values)
# It will look like this
pd.DataFrame ({'Primary': mydata['Primary'].values, 'Color': t_color}).tail(5)
c:\users\adrian\appdata\local\programs\python\python36\lib\site-packages\numpy\core\_methods.py:29: RuntimeWarning: invalid value encountered in reduce
return umr_minimum(a, axis, None, out, keepdims)
Out[6]:
Color
Primary
135
yellow
22.75
136
lime
9.42
137
lime
8.48
138
lime
2.82
139
lime
5.26
In [7]:
# now we plot the colors
tabl2= ct.HTMLTable()
tabl2.addcolumn(name= "Index",
text = mydata.index.values ,
bar= None,
colour=None,
overwrite=True)
tabl2.addcolumn(name= "Primary",
text = mydata['Primary'].values ,
bar= mydata['Primary'].values,
colour=t_color,
overwrite=True)
tabl2.addcolumn(name= "Secondary",
text = mydata['Secondary'].values ,
bar= mydata['Secondary'].values,
colour=s_color,
overwrite=True)
tabl2.display()
Index
Primary
Secondary
0
0.06
0.22
1
0.06
0.27
2
0.08
0.4
3
0.09
0.39
4
0.09
0.24
5
0.1
0.48
6
0.1
0.21
7
0.11
0.36
8
0.11
0.22
9
0.16
0.3
10
0.16
0.31
11
0.17
0.3
12
0.18
1.6
13
0.19
0.59
14
0.19
0.18
15
0.19
0.75
16
0.22
0.44
17
0.24
0.58
18
0.26
0.54
19
0.28
0.62
20
0.28
0.97
21
0.31
2.88
22
0.32
0.35
23
0.33
0.48
24
0.34
0.48
25
0.34
0.23
26
0.34
0.51
27
0.4
0.47
28
0.45
0.74
29
0.46
2.52
30
0.51
1.21
31
0.57
5.55
32
0.62
2.3
33
0.65
1.06
34
0.67
0.43
35
0.71
2.07
36
0.79
1.4
37
0.81
0.8
38
0.83
0.72
39
0.84
0.6
40
0.89
1.01
41
0.92
0.99
42
0.93
1.29
43
0.94
1.06
44
0.96
0.66
45
0.99
0.52
46
0.99
1.53
47
1.01
0.67
48
1.02
0.66
49
1.1
1.09
50
1.11
0.42
51
1.21
2.16
52
1.21
2.07
53
1.27
0.61
54
1.34
2.15
55
1.36
2.58
56
1.37
1.45
57
1.38
0.99
58
1.38
1.37
59
1.66
4.68
60
1.7
1.3
61
1.71
3.61
62
1.78
6.25
63
1.81
1.37
64
1.82
4.73
65
1.89
1.89
66
1.96
3.47
67
1.98
4.96
68
2.13
1.8
69
2.17
1.41
70
2.33
1.98
71
2.34
1.25
72
2.47
1.8
73
2.75
2.65
74
2.76
7.66
75
2.84
3.66
76
2.99
2.74
77
3.04
1.66
78
3.33
1.89
79
3.35
2.94
80
3.51
0.87
81
3.81
4.44
82
4.6
4.7
83
4.89
2.1
84
5.05
5.18
85
5.15
2.19
86
5.31
3.29
87
6.26
17.02
88
6.41
2.45
89
6.49
14.95
90
7.53
10.21
91
8.03
5.21
92
8.34
8.02
93
9.08
3.32
94
10.27
5.67
95
17.19
10.1
96
18.76
10.76
97
3.64
3.86
98
3.59
4.99
99
9.08
1.68
100
2.22
3.23
101
7.56
19.74
102
8.9
12.32
103
2.55
15.91
104
7.92
12.01
105
4.29
2.49
106
2.33
2.3
107
3.21
2.75
108
7.71
2.82
109
12.74
14.04
110
15.77
22.46
111
20.35
14.09
112
5.54
9.0
113
5.38
9.08
114
15.77
11.87
115
10.2
9.61
116
9.01
4.38
117
9.27
4.4
118
3.56
5.84
119
2.52
6.19
120
18.64
11.79
121
7.94
7.09
122
2.28
9.32
123
2.51
4.96
124
19.44
3.6
125
2.96
3.17
126
2.97
3.62
127
4.92
2.86
128
5.54
5.92
129
8.71
11.72
130
2.74
4.07
131
3.61
5.03
132
58.32
10.26
133
11.08
9.31
134
21.08
10.26
135
22.75
8.21
136
9.42
6.76
137
8.48
12.78
138
2.82
9.21
139
5.26
12.4
In [ ]:
Content source: opengeostat/pygslib
Similar notebooks: