1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module for data plotting using gnuplot."""
24
25
26 from os import chmod
27 from stat import S_IRWXU, S_IRGRP, S_IROTH
28
29
30 from lib.io import file_root, get_file_path, open_write_file, swap_extension
31 from lib.plotting import text
32
33
35 """Gnuplot plotting function for representing correlation matrices.
36
37 @keyword matrix: The correlation matrix. This must be a square matrix.
38 @type matrix: numpy rank-2 array.
39 @keyword labels: The labels for each element of the matrix. The same label is assumed for each [i, i] pair in the matrix.
40 @type labels: list of str
41 @keyword file: The name of the file to create.
42 @type file: str
43 @keyword dir: The directory where the PDB file will be placed. If set to None, then the file will be placed in the current directory.
44 @type dir: str or None
45 """
46
47
48 n = len(matrix)
49
50
51 text.correlation_matrix(matrix=matrix, labels=labels, file=file, dir=dir, force=force)
52
53
54 file_name = swap_extension(file=file, ext='gnu')
55
56
57 output = open_write_file(file_name, dir=dir, force=force)
58
59
60 output.write("#!/usr/bin/env gnuplot\n\n")
61
62
63
64 output.write("# Set up the terminal type and make the plot square.\n")
65 output.write("set terminal postscript eps size 10,10 enhanced color font 'Helvetica,20' linewidth 0.1\n")
66 output.write("set size square\n")
67
68
69 output.write("\n# Blue-red colour map.\n")
70 output.write("set palette model RGB\n")
71 output.write("set palette defined\n")
72
73
74 if labels != None:
75 output.write("\n# Labels.\n")
76 for axis in ['x', 'y']:
77 output.write("set %stics out " % axis)
78 if axis == 'x':
79 output.write("rotate ")
80 output.write("font \",8\" (")
81 for i in range(n):
82 if i != 0:
83 output.write(", ")
84 output.write("\"%s\" %s" % (format_enhanced(labels[i]), i))
85 output.write(")\n")
86
87
88 output.write("\n# Output to EPS.\n")
89 output.write("set output \"%s.eps\"\n" % file_root(file))
90
91
92 output.write("\n# Load and show the text data\n")
93 output.write("plot \"%s\" matrix with image\n" % file)
94
95
96 output.close()
97
98
99 chmod(get_file_path(file_name=file_name, dir=dir), S_IRWXU|S_IRGRP|S_IROTH)
100
101
116