-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotting.py
36 lines (31 loc) · 1.18 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
"""
Short Plotting Routine to Plot Pandas Dataframes by Column Label
1. Takes list of dateframes to compare multiple trials
2. Takes list of y-variables to combine on 1 plot
3. Legend location and y-axis limits can be customized
Written by Patrick Coady (pat-coady.github.io)
"""
import matplotlib.pyplot as plt
def df_plot(dfs, x, ys, ylim=None, legend_loc='best'):
""" Plot y vs. x curves from pandas dataframe(s)
Args:
dfs: list of pandas dataframes
x: str column label for x variable
ys: list of str column labels for y variable(s)
ylim: tuple to override automatic y-axis limits
legend_loc: str to override automatic legend placement:
'upper left', 'lower left', 'lower right' , 'right' ,
'center left', 'center right', 'lower center',
'upper center', and 'center'
"""
if ylim is not None:
plt.ylim(ylim)
for df, name in dfs:
if '_' in name:
name = name.split('_')[1]
for y in ys:
plt.plot(df[x], df[y], linewidth=1,
label=name + ' ' + y.replace('_', ''))
plt.xlabel(x.replace('_', ''))
plt.legend(loc=legend_loc)
plt.show()