6
6
import argparse
7
7
8
8
parser = argparse .ArgumentParser ()
9
- parser .add_argument ("--inputfile" ,type = str )
10
- parser .add_argument ("--sizeWorld" ,type = int ,default = 1 )
11
- parser .add_argument ("--show" ,type = int ,choices = [0 ,1 ],default = 1 )
12
- parser .add_argument ("--save" ,type = str ,default = "" )
9
+ parser .add_argument ("--inputfile" , type = str )
10
+ parser .add_argument ("--show" , type = int , choices = [0 , 1 ], default = 1 )
11
+ parser .add_argument ("--save" , type = str , default = "" )
13
12
14
13
args = parser .parse_args ()
15
14
inputfile = args .inputfile
16
- sizeWorld = args .sizeWorld
17
15
18
16
# First Data
19
- size = pd .read_csv (inputfile + "_0.csv" , nrows = 1 ,header = None )
17
+ size = pd .read_csv (inputfile , nrows = 1 , header = None )
20
18
nr = size .iloc [0 ][0 ]
21
19
nc = size .iloc [0 ][1 ]
22
- matrix = np .zeros ((nr ,nc ))
20
+ matrix = np .zeros ((nr , nc ))
23
21
24
22
# Figure
25
- fig , axes = plt .subplots (1 ,1 )
23
+ fig , axes = plt .subplots (1 , 1 )
26
24
axes .xaxis .tick_top ()
27
25
plt .imshow (matrix )
28
26
29
27
# Issue: there a shift of one pixel along the y-axis...
30
- shift = axes .transData .transform ([(0 ,0 ), (1 ,1 )])
31
- shift = shift [1 ,1 ] - shift [0 ,1 ] # 1 unit in display coords
28
+ shift = axes .transData .transform ([(0 , 0 ), (1 , 1 )])
29
+ shift = shift [1 , 1 ] - shift [0 , 1 ] # 1 unit in display coords
32
30
shift = 0
33
31
# 1/shift # 1 pixel in display coords
34
32
35
33
# Loop
36
- for i in range (0 ,sizeWorld ):
37
- print (i )
38
- data = pd .read_csv (inputfile + "_" + str (i )+ ".csv" ,skiprows = 1 ,header = None )
39
-
40
- for index , row in data .iterrows ():
41
- matrix [np .ix_ (range (row [0 ],row [0 ]+ row [1 ]),range (row [2 ],row [2 ]+ row [3 ]))]= row [4 ]
42
- rect = patches .Rectangle ((row [2 ]- 0.5 ,row [0 ]- 0.5 + shift ),row [3 ],row [1 ],linewidth = 0.75 ,edgecolor = 'k' ,facecolor = 'none' )
43
- axes .add_patch (rect )
44
- if row [4 ]>= 0 and row [3 ]/ float (nc )> 0.05 and row [1 ]/ float (nc )> 0.05 :
45
- axes .annotate (row [4 ],(row [2 ]+ row [3 ]/ 2. ,row [0 ]+ row [1 ]/ 2. ),color = "white" ,size = 10 , va = 'center' , ha = 'center' )
46
-
47
-
48
-
34
+ data = pd .read_csv (inputfile , skiprows = 1 , header = None )
35
+ for index , row in data .iterrows ():
36
+ matrix [
37
+ np .ix_ (range (row [0 ], row [0 ] + row [1 ]), range (row [2 ], row [2 ] + row [3 ]))
38
+ ] = row [4 ]
39
+ rect = patches .Rectangle (
40
+ (row [2 ] - 0.5 , row [0 ] - 0.5 + shift ),
41
+ row [3 ],
42
+ row [1 ],
43
+ linewidth = 0.75 ,
44
+ edgecolor = "k" ,
45
+ facecolor = "none" ,
46
+ )
47
+ axes .add_patch (rect )
48
+ if row [4 ] >= 0 and row [3 ] / float (nc ) > 0.05 and row [1 ] / float (nc ) > 0.05 :
49
+ axes .annotate (
50
+ row [4 ],
51
+ (row [2 ] + row [3 ] / 2.0 , row [0 ] + row [1 ] / 2.0 ),
52
+ color = "white" ,
53
+ size = 10 ,
54
+ va = "center" ,
55
+ ha = "center" ,
56
+ )
49
57
50
58
51
59
# Colormap
52
60
def truncate_colormap (cmap , minval = 0.0 , maxval = 1.0 , n = 100 ):
53
61
new_cmap = colors .LinearSegmentedColormap .from_list (
54
- 'trunc({n},{a:.2f},{b:.2f})' .format (n = cmap .name , a = minval , b = maxval ),
55
- cmap (np .linspace (minval , maxval , n )))
62
+ "trunc({n},{a:.2f},{b:.2f})" .format (n = cmap .name , a = minval , b = maxval ),
63
+ cmap (np .linspace (minval , maxval , n )),
64
+ )
56
65
return new_cmap
57
66
58
- cmap = plt .get_cmap ('YlGn' )
67
+
68
+ cmap = plt .get_cmap ("YlGn" )
59
69
new_cmap = truncate_colormap (cmap , 0.4 , 1 )
60
70
61
71
62
72
# Plot
63
- matrix = np .ma .masked_where (0 > matrix ,matrix )
73
+ matrix = np .ma .masked_where (0 > matrix , matrix )
64
74
new_cmap .set_bad (color = "red" )
65
- plt .imshow (matrix ,cmap = new_cmap ,vmin = 0 , vmax = 10 )
75
+ plt .imshow (matrix , cmap = new_cmap , vmin = 0 , vmax = 10 )
66
76
67
77
# Output
68
78
if args .show :
69
79
plt .show ()
70
80
71
81
72
- if args .save != "" :
73
- fig .savefig (args .save ,bbox_inches = 'tight' ,
74
- pad_inches = 0 )
82
+ if args .save != "" :
83
+ fig .savefig (args .save , bbox_inches = "tight" , pad_inches = 0 )
0 commit comments