/
plotting.py
174 lines (136 loc) · 6.37 KB
/
plotting.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" Short description of this Python module.
Longer description of this module.
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/>.
"""
__author__ = "Daniel Martin-Yerga"
__email__ = "dyerga@gmail.com"
__copyright__ = "Copyright 2018"
__license__ = "GPLv3"
__program__ = "plotSciFigs"
__version__ = "0.0.1"
import matplotlib as mpl
mpl.use("Qt5Agg")
import numpy as np
from plotstyle import PlotStyle
import matplotlib.pyplot as plt
from extractdata import ExtractData
from lineplot import LinePlot
from calplot import CalPlot
class Plotting():
def __init__(self, config, plotsconfig, annotations):
self.nplots, self.singlecolumn, self.verticalplot, self.plotstyle = config
if self.verticalplot:
self.singlecolumn = True
plotstyle = PlotStyle(self.singlecolumn, self.nplots, self.verticalplot, self.plotstyle)
_params = plotstyle.get_params()
mpl.rc_context(rc=_params)
fig, axnumber = self.createFigure()
#0: self.plottype, 1:self.filename, 2:self.xlabel1, 3:self.ylabel1, 4:self.ylabel2,
# 5: self.legends, 6:xlimit1, 7:ylimi1, 8:ylimit2, 9: doubleaxis, 10: legend1loc, 11: legend2loc,
# 12: xaxislocator, 13: y1axislocator, 14: y2axislocator, 15: converty, 16: converty2, 17: normalized,
# 18: desvest, 19: lineplot, 20: multicolor = plotsconfig
self.plotTextAnnotations(plt, annotations)
for nplot in range(self.nplots):
print("printing plot: ", nplot + 1)
plottype = plotsconfig[nplot][0]
plotfilename = plotsconfig[nplot][1]
print("filename: ", plotfilename)
plotdata = self.getPlotData(plottype, plotfilename, plotsconfig[nplot][9], plotsconfig[nplot][15],
plotsconfig[nplot][16], plotsconfig[nplot][17], plotsconfig[nplot][18])
try:
naxis = axnumber[nplot]
except TypeError:
naxis = axnumber
other_anns = self.getOtherAnnotations(annotations, nplot+1)
plot = self.plotPlot(plottype, plotdata, plotsconfig[nplot], naxis, other_anns)
# TODO: remove when creating customized figures
plt.tight_layout()
# plt.savefig('test2.png', format='png')
plt.show()
def plotTextAnnotations(self, plt, anns):
for i in range(len(anns)):
if anns[i][0] == "Text":
plt.gcf().text(x=float(anns[i][2]), y=float(anns[i][3]), s=anns[i][1])
def getOtherAnnotations(self, anns, nplot):
other_anns = []
anntypes = ["Arrow", "Ellipse"]
for i in range(len(anns)):
if anns[i][1] in anntypes:
if int(anns[i][0]) == nplot:
other_anns.append(anns[i])
return other_anns
def getPlotData(self, plottype, plotfilename, doubleaxis, converty, converty2, normalized, desvest):
exdata = ExtractData(plottype, plotfilename, doubleaxis, desvest)
#xdata, ydata, xdata2, ydata2
plotdata = exdata.get_data()
#print("convert; ", converty, " ", converty2)
#TODO: smoothing data
if converty:
for i in range(len(plotdata[1])):
plotdata[1][i] = np.array(plotdata[1][i]).astype(float) * float(converty)
if converty2:
for i in range(len(plotdata[3])):
plotdata[3][i] = np.array(plotdata[3][i]).astype(float) * float(converty2)
if normalized == "Normalized by max":
for i in range(len(plotdata[1])):
maxvalue = max(plotdata[1][i])
for j in range(len(plotdata[1][i])):
plotdata[1][i][j] = plotdata[1][i][j] / maxvalue
elif normalized == "Normalized by first":
for i in range(len(plotdata[1])):
firstvalue = plotdata[1][i][0]
for j in range(len(plotdata[1][i])):
plotdata[1][i][j] = plotdata[1][i][j] / firstvalue
return plotdata
def plotPlot(self, plottype, plotdata, plotconfig, axis, anns):
#xdata1, ydata1, xdata2, ydata2 = plotdata
if plottype == "LinePlot" or plottype == "DotPlot":
lineplot = LinePlot(plotconfig)
lineplot.plot(axis, plotdata, anns)
elif plottype == "CalPlot":
calplot = CalPlot(plotconfig)
calplot.plot(axis, plotdata, anns)
def createFigure(self):
#TODO: create special 2+1 single column figures, etc.
# fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
# axnumber = [ax1, ax2, ax3, ax4]
if self.nplots == 1:
fig, axnumber = plt.subplots(1, 1)
elif self.nplots == 2:
if self.verticalplot:
fig, axnumber = plt.subplots(2, 1)
else:
fig, axnumber = plt.subplots(1, 2)
elif self.nplots == 3:
if self.verticalplot:
fig, axnumber = plt.subplots(3, 1)
# TODO: create customized figures
# fig = plt.figure()
# ax1 = fig.add_axes([0.2, .72, .6, .25])
# ax2 = fig.add_axes([0.08, .34, .85, .32])
# ax3 = fig.add_axes([0.08, .01, .85, .32])
# axnumber = [ax1, ax2, ax3]
else:
fig, axnumber = plt.subplots(1, 3)
# fig = plt.figure()
# ax1 = fig.add_axes([0.07, .21, .25, .75])
# ax2 = fig.add_axes([0.33, .21, .25, .75])
# ax3 = fig.add_axes([0.66, .02, .35, .95])
# axnumber = [ax1, ax2, ax3]
elif self.nplots == 4:
if self.verticalplot:
fig, axnumber = plt.subplots(4, 1)
else:
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
axnumber = [ax1, ax2, ax3, ax4]
return fig, axnumber