Newer
Older
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
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
from plottingabc import BasePlotter
class _MeanPlotter(BasePlotter):
"""Draw mean time series plus standard error."""
def __init__(
self,
trace_df,
trace_name,
unit_scaling,
label,
mean_color,
error_color,
mean_linestyle,
mean_marker,
xlabel,
ylabel,
plot_title,
):
super().__init__(trace_name, unit_scaling, xlabel, plot_title)
# Define attributes from arguments
self.trace_df = trace_df
self.label = label
self.mean_color = mean_color
self.error_color = error_color
self.mean_linestyle = mean_linestyle
self.mean_marker = mean_marker
# Define some labels
self.ylabel = ylabel
# Mean and standard error
self.trace_time = (
np.array(self.trace_df.columns, dtype=float) * self.unit_scaling
)
self.mean_ts = self.trace_df.mean(axis=0)
self.stderr = self.trace_df.std(axis=0) / np.sqrt(len(self.trace_df))
def plot(self, ax):
"""Draw lines and shading on provided Axes."""
super().plot(ax)
ax.plot(
self.trace_time,
self.mean_ts,
color=self.mean_color,
alpha=0.75,
linestyle=self.mean_linestyle,
marker=self.mean_marker,
label="Mean, " + self.label,
)
ax.fill_between(
self.trace_time,
self.mean_ts - self.stderr,
self.mean_ts + self.stderr,
color=self.error_color,
alpha=0.5,
label="Standard error, " + self.label,
)
ax.legend(loc="upper right")
def mean_plot(
trace_df,
trace_name="flavin",
unit_scaling=1,
label="wild type",
mean_color="b",
error_color="lightblue",
mean_linestyle="-",
mean_marker="",
xlabel="Time (min)",
ylabel="Normalised flavin fluorescence (AU)",
plot_title="",
ax=None,
):
"""Plot mean time series of a DataFrame, with standard error shading.
Parameters
----------
trace_df : pandas.DataFrame
Time series of traces (rows = cells, columns = time points).
trace_name : string
Name of trace being plotted, e.g. 'flavin'.
unit_scaling : int or float
Unit scaling factor, e.g. 1/60 to convert minutes to hours.
label : string
Name of group being plotted, e.g. a strain name.
mean_color : string
matplotlib colour string for the mean trace.
error_color : string
matplotlib colour string for the standard error shading.
mean_linestyle : string
matplotlib linestyle argument for the mean trace.
mean_marker : string
matplotlib marker argument for the mean trace.
xlabel : string
x axis label.
ylabel : string
y axis label.
plot_title : string
Plot title.
ax : matplotlib Axes
Axes in which to draw the plot, otherwise use the currently active Axes.
Examples
--------
FIXME: Add docs.
"""
plotter = _MeanPlotter(
trace_df,
trace_name,
unit_scaling,
label,
mean_color,
error_color,
mean_linestyle,
mean_marker,
xlabel,
ylabel,
plot_title,
)
if ax is None:
ax = plt.gca()
plotter.plot(ax)
return ax