-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
71 lines (53 loc) · 1.52 KB
/
plot.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
import argparse
import datetime
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
parser = argparse.ArgumentParser()
parser.add_argument(
'-i', '--input',
type=str,
required=True,
help='input CSV file'
)
parser.add_argument(
'-w', '--window',
type=int,
default=30,
help='moving average window (default: 30)'
)
parser.add_argument(
'-n', '--name',
type=str,
default='Plant',
help='specific plant type name (default: Plant)'
)
args = parser.parse_args()
df = pd.read_csv(args.input)
states = df.drop_duplicates(['state'])['state']
final = pd.DataFrame()
for state in states:
aggregate = df[df['state'] == state].groupby(['date']).sum()
aggregate = aggregate.rename(columns={
'weight': state
})
final = pd.concat([final, aggregate], axis=1, sort=True)
# Fill missing data points
final = final.interpolate(limit_direction='both')
final = final.rolling(args.window).mean()
# Convert index to date
final.index = pd.to_datetime(final.index)
ax = final.plot()
ax.autoscale(tight=False)
ax.set_title(args.name)
ax.set_ylabel('Phase cumulative weight')
ax.xaxis.set_major_locator(mdates.MonthLocator(interval=2))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax.set_xlim([datetime.date(2019, 1, 1), datetime.date(2020, 7, 15)])
plt.margins(0)
plt.legend(loc='upper left')
plt.tight_layout()
# plt.gca().get_legend().remove()
plt.show()