An error occurred while loading the file. Please try again.
-
Pierre-Antoine Rouby authorede3555b2c
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
# Plot.py -- Pamhyr
# Copyright (C) 2023 INRAE
#
# 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 <https://www.gnu.org/licenses/>.
# -*- coding: utf-8 -*-
from datetime import datetime
from tools import timer, trace
from View.Tools.PamhyrPlot import PamhyrPlot
from PyQt5.QtCore import (
QCoreApplication
)
from View.LateralContribution.Edit.translate import *
_translate = QCoreApplication.translate
class Plot(PamhyrPlot):
def __init__(self, mode="time", data=None,
canvas=None, trad=None, toolbar=None,
parent=None):
super(Plot, self).__init__(
canvas=canvas,
trad=trad,
data=data,
toolbar=toolbar,
parent=parent
)
self._table_headers = self._trad.get_dict("table_headers")
self._mode = mode
def custom_ticks(self):
if self.data.header[0] != "time":
return
t0 = datetime.fromtimestamp(0)
nb = len(self.data.data)
mod = int(nb / 5)
mod = mod if mod > 0 else nb
fx = list(
map(
lambda x: x[1],
filter(
lambda x: x[0] % mod == 0,
enumerate(self.data.data)
)
)
)
xx = list(map(lambda v: v[0], fx))
if self._mode == "time":
xt = list(
map(
lambda v: str(
datetime.fromtimestamp(v[0]) - t0
).split(",")[0]
.replace("days", _translate("LateralContribution", "days"))
.replace("day", _translate("LateralContribution", "day")),
fx
)
)
else:
xt = list(
map(
lambda v: str(datetime.fromtimestamp(v[0]).date()),
fx
)
)
self.canvas.axes.set_xticks(ticks=xx, labels=xt, rotation=45)
@timer
def draw(self):
self.canvas.axes.cla()
self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5)
if len(self.data) == 0:
self._init = False
return
# Plot data
x = list(map(lambda v: v[0], self.data.data))
y = list(map(lambda v: v[1], self.data.data))
self._line, = self.canvas.axes.plot(
x, y,
color='r', lw=1.,
markersize=5, marker='+',
picker=30,
)
self.custom_ticks()
# Plot label
header = self.data.header
self.canvas.axes.set_xlabel(
self._table_headers[header[0]], color='black', fontsize=10
)
self.canvas.axes.set_ylabel(
self._table_headers[header[1]], color='black', fontsize=10
)
self.canvas.axes.autoscale_view(True, True, True)
self.canvas.figure.tight_layout()
self.canvas.figure.canvas.draw_idle()
# self.toolbar.update()
self._init = True
@timer
def update(self, ind=None):
if not self._init:
self.draw()
return
x = list(map(lambda v: v[0], self.data.data))
y = list(map(lambda v: v[1], self.data.data))
self._line.set_data(x, y)
self.custom_ticks()
self.canvas.axes.relim()
self.canvas.axes.autoscale()
self.canvas.figure.tight_layout()
self.canvas.figure.canvas.draw_idle()