-
Notifications
You must be signed in to change notification settings - Fork 2
/
plotfx.py
120 lines (111 loc) · 4.52 KB
/
plotfx.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
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
def single_plot_xtime(df, metric, title, ylabel):
"""
Generates a single plot of results over time; includes separate grey
lines for each run and a single blue line for the average of all runs
-------
Inputs
-------
df: pandas DataFrame
Dataframe to use for plotting
metric: string
Name of dataframe column to use for plotting
title: string
Plot title
ylabel: string
Y-axis label
"""
mpl.rcParams['axes.titlesize'] = 'x-large'
mpl.rcParams['axes.labelsize'] = 'x-large'
for i in range(1, df['rep'].max() + 1):
plt.plot(df[df.rep == i]['tstep'].values, df[df.rep == i][metric].values, 'lightgrey', linewidth = 0.75)
plt.plot(df.groupby('tstep')[metric].mean().index, df.groupby('tstep')[metric].mean().values)
plt.title(title)
plt.xlabel('Time (Days)')
plt.ylabel(ylabel)
plt.show();
def row_plot_xtime_bytest(frames, metric, ylabel, titles, suptitle = None):
"""
Generates a row of plots for a single metric over time; each plot represents
a different dataframe; includes separate grey lines for
each run and a single blue line for the average of all runs
-------
Inputs
-------
frames: list of pandas DataFrame
Dataframes to use for plotting, each will be plotted in a separate
column
metric: string
Name of dataframe column to use for plotting
ylabel: string
Y-axis label
titles: list of strings
Plot titles; one for each dataframe/column
suptitle: string, optional
Main title
"""
mpl.rcParams['axes.titlesize'] = 'x-large'
mpl.rcParams['axes.labelsize'] = 'x-large'
f, axs = plt.subplots(1, len(frames), figsize = (20, 5), sharey = True)
f.suptitle(suptitle, fontsize = 15)
f.subplots_adjust(wspace=0, hspace=0)
for (i,df) in enumerate(frames):
for j in range(1, df['rep'].max() + 1):
axs[i].plot(df[df.rep == j]['tstep'].values, df[df.rep == j][metric].values, 'lightgrey', linewidth = 0.75)
axs[i].plot(df.groupby('tstep')[metric].mean().index, df.groupby('tstep')[metric].mean().values)
axs[i].set_title(titles[i])
axs[i].set_xlabel('Time (Days)')
if i==0:
axs[i].set_ylabel(ylabel)
def grid_plot_xtime_bytest(frame, row_metric, col_metric, plot_metric, ylabel, titles, suptitle = None):
"""
Generates a grid of plots over time; each row and column represents a
different metric; includes separate grey lines for each run and a single
blue line for the average of all runs
-------
Inputs
-------
frame: pandas DataFrame
Dataframe to use for plotting
row_metric: string
Name of dataframe column to use for the row; each unique value will be
plotted in a separate row
col_metric: string
Name of dataframe column to use for the column; each unique value will
be plotted in a separate column
plot_metric: string
Name of dataframe column to use for plotting
ylabel: string
Y-axis label
titles: list of strings
Plot titles; one for each column
suptitle: string, optional
Main title
"""
mpl.rcParams['axes.titlesize'] = 'x-large'
mpl.rcParams['axes.labelsize'] = 'x-large'
nrows = frame[row_metric].nunique()
ncols = frame[col_metric].nunique()
f, axs = plt.subplots(nrows, ncols, figsize = (20, 15), sharex = True, sharey = True)
f.suptitle(suptitle, fontsize = 20, y = 0.93)
f.subplots_adjust(wspace=0, hspace=0)
# Loop through the rows
for (i, row) in enumerate(frame[row_metric].unique()):
# Loop through the columns
for (j, col) in enumerate(frame[col_metric].unique()):
subdat = frame.loc[(frame[row_metric]==row) & (frame[col_metric]==col)]
for k in range(1, subdat['rep'].max() + 1):
axs[i,j].plot(subdat[subdat.rep == k]['tstep'].values, subdat[subdat.rep == k][plot_metric].values, 'lightgrey', linewidth = 0.75)
axs[i,j].plot(subdat.groupby('tstep')[plot_metric].mean().index, subdat.groupby('tstep')[plot_metric].mean().values)
# Only set title if first row
if i==0:
axs[i,j].set_title(titles[j])
# Only set xlabel if last row
if i==(nrows-1):
axs[i,j].set_xlabel('Time (Days)')
# Only set ylabel if first column
if j==0:
axs[i,j].set_ylabel(ylabel)