-
Notifications
You must be signed in to change notification settings - Fork 0
/
private.py
133 lines (95 loc) · 3.89 KB
/
private.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
# Copyright (C) 2018 István Bozsó
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sys import platform, stderr
from os import linesep, popen, remove
from numpy import dtype
from math import ceil, sqrt
from weakref import ref
import subprocess as sub
class MultiPlot(object):
def __init__(self, nplot, session, **kwargs):
self.session = ref(session)()
common_keys = bool(kwargs.get("ckeys", False))
if common_keys:
nplot += 1
self.nplot = nplot
self.left = float(kwargs.get("left", 20)) / 100.0
self.between = float(kwargs.get("between", 20.0)) / 100.0
self.width = float(kwargs.get("width", 30.0)) / 100.0
self.key_height = float(kwargs.get("key_height", 50.0)) / 100.0
self.width_total = self.width * nplot + self.between * (nplot - 1) \
+ self.left + self.key_height
nrows = kwargs.get("nrows", None)
if nrows is None:
nrows = max([1, ceil(sqrt(self.nplot) - 1)])
ncols = ceil(self.nplot / nrows)
portrait = kwargs.get("portrait", False)
if portrait and ncols > nrows:
nrows, ncols = ncols, nrows
elif nrows > ncols:
nrows, ncols = ncols, nrows
self.nrows, self.ncols = nrows, ncols
order = kwargs.get("order", "rowsfirst")
title = kwargs.get("title", None)
if title is not None:
txt = "set multiplot layout {},{} {} title '{}'"\
.format(nrows, ncols, order, title)
else:
txt = "set multiplot layout {},{} {}".format(nrows, ncols, order)
if common_keys:
txt += "; unset key"
self.session(txt)
def __call__(self, *args):
self.session(*args)
def key(self, *titles):
self("unset title")
plot = ", ".join("2 title '%s'" % title for title in titles)
self(
"""
set lmargin at screen %g
set rmargin at screen 1
set key center center
set border 0
unset tics
unset xlabel
unset ylabel
set yrange [0:1]
plot %s
""" % (self.margins(self.nplot)[0] - 0.35, plot))
def plot(self, ii, txt):
sess = self.session
if ii > 0:
self("unset ylabel")
self("set lmargin at screen %g\nset rmargin at screen %g" % self.margins(ii))
sess(txt)
def margins(self, ii):
l, b, w = self.left, self.between, self.width
left = ii * (w + b) + l
return left, left + w
def __del__(self):
self.session("unset multiplot")
self.session.multi = None
#if platform == "mac":
#from gnuplot_platforms import GnuplotProcessMac as gpp
#elif platform == "win32" or platform == "cli":
#from gnuplot_platforms import GnuplotProcessWin32 as gpp
#elif platform == "darwin":
#from gnuplot_platforms import GnuplotProcessMacOSX as gpp
#elif platform[:4] == "java":
#from gnuplot_platforms import GnuplotProcessJava as gpp
#elif platform == "cygwin":
#from gnuplot_platforms import GnuplotProcessCygwin as gpp
#else:
#from gnuplot_platforms import GnuplotProcessUnix as gpp