-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
86 lines (60 loc) · 2.63 KB
/
plot.py
File metadata and controls
86 lines (60 loc) · 2.63 KB
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
"""
Reads and plots data from csv
"""
import logging
import plotly.express
from plotly.subplots import make_subplots
from .exceptions import PlotTorqueProException
from .functional import lfilter
logger = logging.getLogger(__name__)
def render_plot(csv_data, plot_config):
configure_axes(csv_data, plot_config)
y2 = plot_config.pop('y2', None)
hovertemplate = plot_config.pop('hovertemplate', None)
hovermode = plot_config.pop('hovermode', None)
fig = plotly.express.line(csv_data, **plot_config)
if y2:
fig = plot_twin_x(csv_data, fig, y2=y2, **plot_config)
if hovertemplate:
fig.update_traces(hovertemplate=hovertemplate)
if hovermode:
fig.update_layout(hovermode=hovermode)
return fig
def plot_twin_x(csv_data, fig, x, y2, **_):
twin_axes = make_subplots(specs=[[dict(secondary_y=True)]])
right_axis = plotly.express.line(csv_data, x=x, y=y2)
right_axis.update_traces(yaxis='y2')
twin_axes.add_traces(fig.data + right_axis.data)
fig = twin_axes
return fig
def configure_axes(csv_dataframe, config):
plot_columns = list(csv_dataframe.columns)
# Make sure we handle the axes
x_axis = config.get('x')
y_axis = config.get('y')
y2_axis = config.get('y2')
if x_axis is None and y_axis is None:
x_axis = plot_columns[0]
y_axis = plot_columns[1:]
if y_axis is None:
y_axis = list(plot_columns)
y_axis.remove(x_axis)
if y2_axis is not None:
y_axis = lfilter(lambda c: c not in set(y2_axis), y_axis)
# Throw errors if necessary
if x_axis not in plot_columns:
logger.error("x-axis is not available. Double check you haven't misspelled a name: x-axis=\"%s\", columns=%s",
x_axis, plot_columns)
raise PlotTorqueProException("axis is not available in csv, either because it's missing or it was excluded")
if any(y not in plot_columns for y in y_axis):
logger.error("y-axis is not available. Double check you haven't misspelled a name: x-axis=\"%s\", columns=%s",
y_axis, plot_columns)
raise PlotTorqueProException("axis is not available in csv, either because it's missing or it was excluded")
if y2_axis and any(y not in plot_columns for y in y2_axis):
logger.error("y2-axis is not available. Double check you haven't misspelled a name: x-axis=\"%s\", columns=%s",
y2_axis, plot_columns)
raise PlotTorqueProException("axis is not available in csv, either because it's missing or it was excluded")
# add to config
config['x'] = x_axis
config['y'] = y_axis
config['y2'] = y2_axis